# _*_ coding : utf-8 _*_

from random import shuffle
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
import pandas as pd
import time
import random
import itertools
from math import radians, cos, sin, asin, sqrt
import sys
import copy
import time

bestJ = 0  # 调用递归函数时要用
bestWay = []  # 调用递归函数时要用
bestLossE1 = 0  # 调用递归函数时要用
bestPool = []  # 调用递归函数时要用

numberTug = 0  # 用于记录tugNum等于1时第二次访问拖船需要拉多少个驳船，以及tugNum等于二时第二艘拖船需要拉多少个驳船
nTug1 = 0  # 用于记录tugNum等于2时第一个拖船第二次访问拖船需要拉几个驳船
nTug2 = 0  # 用于记录tugNum等于2时第二个拖船第二次访问拖船需要拉几个驳船

# 用于更新comNum
def find_nth_occurrence(lst, element, n):
    count = 0
    for i, e in enumerate(lst):
        if e == element:
            count += 1
            if count == n:
                return i
    return -1


# 用于加快代码效率
def haveAny1(i, way):
    for any in way:
        if any < 2 * orderNumberF:
            if any % 2 == 0:
                if orderF[int((any) / 2), 5] < orderE1[i - 2 * orderNumberF, 7]:
                    return True
                else:
                    continue
            else:
                if orderF[int((any) / 2), 7] < orderE1[i - 2 * orderNumberF, 7]:
                    return True
                else:
                    continue
        elif any < 2 * orderNumberF + orderNumberE1:
            if orderE1[any - 2 * orderNumberF, 7] < orderE1[i - 2 * orderNumberF, 7]:
                return True
            else:
                continue
        else:
            if orderE2[any - 2 * orderNumberF - orderNumberE1, 9] < orderE1[i - 2 * orderNumberF, 7]:
                return True
            else:
                continue
    return False

#用于加快代码效率
def haveAny2(i, way):
    for any in way:
        if any < 2 * orderNumberF:
            if any % 2 == 0:
                if orderF[int((any) / 2), 5] < orderE2[i - 2 * orderNumberF - orderNumberE1, 9]:
                    return True
                else:
                    continue
            else:
                if orderF[int((any) / 2), 7] < orderE2[i - 2 * orderNumberF - orderNumberE1, 9]:
                    return True
                else:
                    continue
        elif any < 2 * orderNumberF + orderNumberE1:
            if orderE1[any - 2 * orderNumberF, 7] < orderE2[i - 2 * orderNumberF - orderNumberE1, 9]:
                return True
            else:
                continue
        else:
            if orderE2[any - 2 * orderNumberF - orderNumberE1, 9] < orderE2[i - 2 * orderNumberF - orderNumberE1, 9]:
                return True
            else:
                continue
    return False

# repairE里面会递归调用cirInsert这个函数
def cirInsert(k, tempSolution, needPool, pos, j, needpool):  # k是在tempSolution插入终点的位置  #needPool=[a,b]

    global bestJ
    global bestWay
    global bestLossE2
    global bestPool

    for poss in range(pos + 1, k + 1):  # 上一个元素之后的元素的插入位置，是在上一个元素和终点之间
        tempNeedPool = clone(needPool)
        tempTempSolution = clone(tempSolution)
        tempK = k
        tempTempSolution.insert(poss, tempNeedPool[0])  # 插入pool中的第一个元素
        # print("tempTempSolution=", tempTempSolution)
        tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
        # print("tempNeedPool=", tempNeedPool)
        # print("k=", k)
        # print("pos=", pos)
        tempK += 1

        if tempNeedPool != []:
            cirInsert(tempK, tempTempSolution, tempNeedPool, poss, j, needpool)
        else:
            temp = []
            for i in range(courierNumber):
                temp.append([])
            temp[j] = tempTempSolution
            tempLoss = lossFunction(temp, startTime, printf=False)
            # print("bestlossE2=", bestLossE2)
            # print("tempLoss=", tempLoss)
            if (tempLoss < bestLossE2):
                bestLossE2 = tempLoss
                bestJ = j
                bestWay = clone(tempTempSolution)
                bestPool = clone(needpool)
                # print("bestPool=", bestPool)
                # print("bestWay=", bestWay)
                # print("bestJ=", bestJ)

def selectAndUseDestroyOperator(destroyWeightF, currentSolution, stepF):  # select and use destroy operators
    destroyOperatorF = -1  # 初始化destroyOperator的值为-1，后面可以将destoryOperator打印出来看看调用了哪个摧毁算子
    print("current solution是", currentSolution)
    sol = clone_runoob(currentSolution)
    destroyRouletteF = np.array(destroyWeightF).cumsum()  # destroyRoulette是destroyWeight的累加
    # print("destroyWeight是", destroyWeight)
    # print("destroyRoulette是", destroyRoulette)
    r = random.uniform(0, max(destroyRouletteF))  # r是0到max(destroyRoulette)之间随机一个实数
    # print("r摧毁是", r)
    for i in range(len(destroyRouletteF)):
        # print("轮盘赌进行一次")
        if destroyRouletteF[i] >= r:
            if i == 0:
                destroyOperatorF = i  # 初始化摧毁因子为1
                print("destroyOperator=", destroyOperatorF)
                sol, removed = destroyF1(sol, stepF)  # 将随机将两个F类型订单对应的节点丢出来 ，removed对应的是被丢出来的两对节点
                destroyUseTimesF[i] += 1
                # print("destroyUseTimes=", destroyUseTimes)
                break
            elif i == 1:
                destroyOperatorF = i
                print("destroyOperator=", destroyOperatorF)
                sol, removed = destroyF2(sol, stepF)
                destroyUseTimesF[i] += 1
                # print("destroyUseTimes=", destroyUseTimes)
                break
    return sol, removed, destroyOperatorF


def selectAndUseRepairOperator(repairWeightF, destroyedSolution, removeList):  # select and use repair operators
    print("destroyedSolution是", destroyedSolution)
    print("removeList是", removeList)
    repairOperatorF = -1  # 初始化修复因子为-1，后面可以将repairOperator打印出来看看调用了哪个修复算子
    repairRouletteF = np.array(repairWeightF).cumsum()  # 将repairWeight累加至repairRoulette
    # print("repairWeight是", repairWeight)
    # print("repairRoulette是", repairRoulette)
    r = random.uniform(0, max(repairRouletteF))  # 在0到max(repairRoulette)之间随机生成一个实数

    # print("r修复是", r)
    for i in range(len(repairRouletteF)):

        if repairRouletteF[i] >= r:
            if i == 0:
                repairOperatorF = i
                print("repairOperatorF=", repairOperatorF)
                destroyedSolution, removeList = repairF1(destroyedSolution, removeList)
                repairUseTimesF[i] += 1
                # print("repairUseTimes=", repairUseTimes)
                break
            elif i == 1:
                repairOperatorF = i
                print("repairOperatorF=", repairOperatorF)
                destroyedSolution, removeList = repairF2(destroyedSolution, removeList)
                repairUseTimesF[i] += 1
                # print("repairUseTimes=", repairUseTimes)
                break
            elif i == 2:
                repairOperatorF = i
                print("repairOperatorF=", repairOperatorF)
                destroyedSolution, removeList = repairF3(destroyedSolution, removeList)
                repairUseTimesF[i] += 1
                # print("repairUseTimes=", repairUseTimes)
                break
    return destroyedSolution, repairOperatorF, removeList


def selectAndUseDestroyOperator2(destroyWeightE, currentSolution,stepE):  # select and use destroy operators
    destroyOperatorE = -1  # 初始化destroyOperator的值为-1，后面可以将destoryOperator打印出来看看调用了哪个摧毁算子
    print("current solution是", currentSolution)
    sol = clone_runoob(currentSolution)
    destroyRouletteE = np.array(destroyWeightE).cumsum()  # destroyRoulette是destroyWeight的累加
    # print("destroyWeight是", destroyWeight)
    # print("destroyRoulette是", destroyRoulette)
    r = random.uniform(0, max(destroyRouletteE))  # r是0到max(destroyRoulette)之间随机一个实数

    print("r=", r)

    # print("r摧毁是", r)
    for i in range(len(destroyRouletteE)):
        print("轮盘赌进行一次")

        # print("repairRoulette[",i,"]=", destroyRoulette[i])

        if destroyRouletteE[i] >= r:
            if i == 0:
                destroyOperatorE = i  # 初始化摧毁因子为1
                print("destroyOperatorE=", destroyOperatorE)
                sol, removed1, removed2 = destroyE1(sol,stepE)  # 将随机将两个F类型订单对应的节点丢出来 ，removed对应的是被丢出来的两对节点
                destroyUseTimesE[i] += 1
                # print("destroyUseTimes=", destroyUseTimes)
                break
            elif i == 1:
                destroyOperatorE = i
                print("destroyOperatorE=", destroyOperatorE)
                sol, removed1, removed2 = destroyE2(sol,stepE)  #################################
                destroyUseTimesE[i] += 1
                # print("destroyUseTimes=", destroyUseTimes)
                break
    return sol, removed1, removed2, destroyOperatorE


def selectAndUseRepairOperator2(repairWeightE, destroyedSolution, removedList1,removedList2):  # select and use repair operators
    repairOperatorE = -1  # 初始化修复因子为-1，后面可以将repairOperator打印出来看看调用了哪个修复算子
    print("destroyedSolution是", destroyedSolution)
    print("removedList1是", removedList1)
    print("removedList2是", removedList2)
    repairRouletteE = np.array(repairWeightE).cumsum()  # 将repairWeight累加至repairRoulette
    # print("repairWeight是", repairWeight)
    # print("repairRoulette是", repairRoulette)
    r = random.uniform(0, max(repairRouletteE))  # 在0到max(repairRoulette)之间随机生成一个实数
    # print("r修复是", r)
    for i in range(len(repairRouletteE)):
        # print("轮盘赌进行一次")
        if repairRouletteE[i] >= r:
            if i == 0:
                repairOperatorE = i
                print("repairOperatorE=", repairOperatorE)
                destroyedSolution, removedList1, removedList2 = repairE1(destroyedSolution, removedList1, removedList2)
                repairUseTimesE[i] += 1
                # print("repairUseTimes=", repairUseTimes)
                break
            elif i == 1:
                repairOperatorE = i
                print("repairOperatorE=", repairOperatorE)
                destroyedSolution, removedList1, removedList2 = repairE2(destroyedSolution, removedList1, removedList2)
                repairUseTimesE[i] += 1
                # print("repairUseTimes=", repairUseTimes)
                break
            elif i==2:
                repairOperatorE = i
                print("repairOperatorE=", repairOperatorE)
                destroyedSolution, removedList1, removedList2 = repairE3(destroyedSolution, removedList1, removedList2)
                repairUseTimesE[i] += 1
                # print("repairUseTimes=", repairUseTimes)
                break
    return destroyedSolution, repairOperatorE, removedList1, removedList2


# 复制多维数组用这个
def clone_runoob(ways):  # 否则复制会变成指针的复制
    to = []
    for way in ways:
        # [0, 1, 10, 11, 20, 21]
        # [2, 3, 12, 13, 22, 23]
        # [4, 5, 14, 15]
        # [6, 7, 16, 17]
        # [8, 9, 18, 19]
        temp = []
        for i in way:
            temp.append(i)
        to.append(temp)
    return to


# 复制单维数组用这个
def clone(way):
    to = []
    for point in way:
        to.append(point)
    return to


##################生成初始解######################
# 随机贪婪生成F类型订单路径
def genF(ways, pool):  # 贪婪重建算子
    # 分别在每个拖船路径中找到插入的最优值，记录下来，然后再比较整个的，找到这个的插入位置
    pool = [i for i in pool if i <= orderNumberF * 2 - 1]

    for i in pool:
        bindex1 = 0  # 路径参数
        bindex2 = 0  # 第一个目标点参数
        bindex3 = 0  # 第二个目标点参数
        index1 = 0  # 存储bestLoss对应的拖船参数
        index2 = 0  # 存储bestLoss对应的F类型订单起点参数
        index3 = 0  # 存储bestLoss对应的F类型订单终点参数
        bestLoss = 0
        bestLossF2 = 0
        if (i % 2 == 0):  # 当对应的是F类型订单起点的时候进入循环
            for j in range(len(ways)):  # 选择拖船
                for k in range(len(ways[j]) + 1):  # 选择起点插入位置
                    tempSolution = clone(ways[j])
                    tempSolution.insert(k, i)  # 在此位置插入起点
                    for l in range(k + 1, len(tempSolution) + 1):  # 选择终点插入位置
                        tempTempSolution = clone(tempSolution)
                        tempTempSolution.insert(l, i + 1)
                        temp = []
                        temp.append(tempTempSolution)
                        tempLoss = lossFunction(temp, startTime, printf=False)
                        if (tempLoss < bestLossF2 or (k == 0 and l == 1)):
                            bestLossF2 = tempLoss
                            index1 = j
                            index2 = k
                            index3 = l
                tempsolution = clone_runoob(ways)
                tempsolution[index1].insert(index2, i)
                tempsolution[index1].insert(index3, i + 1)
                if (j == 0):
                    bestLoss = lossFunction(tempsolution, startTime, printf=False)
                    bindex1 = index1
                    bindex2 = index2
                    bindex3 = index3
                else:
                    tempLoss = lossFunction(tempsolution, startTime, printf=False)
                    # if (i == 4) and (j==1):
                    # print("j=", j)
                    # print("tempLoss=", tempLoss)
                    # print("bestloss=", bestLoss)
                    # if (i == 4) and (j == 2):
                    #     print("j=", j)
                    #     print("tempLoss=", tempLoss)
                    #     print("bestloss=", bestLoss)
                    if (tempLoss < bestLoss):
                        bestLoss = tempLoss
                        bindex1 = index1
                        bindex2 = index2
                        bindex3 = index3
                if ways[j] == []:
                    break
            ways[bindex1].insert(bindex2, i)
            ways[bindex1].insert(bindex3, i + 1)
    pool = []
    return ways


# 自适应规模-随机贪婪生成E类型订单路径
def genE(ways, pool1, pool2):  # 贪婪重建算子

    # 分别在每个拖船路径中找到插入的最优值，记录下来，然后再比较整个的，找到这个的插入位置

    # print("pool1=",pool1)
    # print("pool2=",pool2)
    # print("ways=",ways)

    global comNum  # 用于记录每个拖船在完成相应的E类型时第一次（如果有第二次的话）拉了几个空驳船

    global tugs  # 一会儿用它结合lossFunction操控拖船

    global bestJ  # 调用递归函数时要用
    global bestWay  # 调用递归函数时要用
    global bestLossE2  # 调用递归函数时要用
    global bestPool  # 调用递归函数时要用

    global numberTug  # 用于记录tugNum等于1时第二次访问拖船需要拉多少个驳船，以及tugNum等于二时第二艘拖船需要拉多少个驳船
    global nTug1  # 用于记录tugNum等于2时第一个拖船第二次访问拖船需要拉几个驳船
    global nTug2  # 用于记录tugNum等于2时第二个拖船第二次访问拖船需要拉几个驳船

    global maxTugNum  # 最多允许几个拖船共同完成一个E类型订单任务（默认为2
    global maxTravelTime  # 最多允许一个拖船访问同一个任务点几次（默认为2
    tempPool1=pool1
    for i in pool2:
        print("i=", i)
        # print("pool1=", pool1)
        # print("pool2=", pool2)
        # print("ways=", ways)
        needNumber = int(orderE1[i - orderNumberF * 2][8])  # 获取E类型订单需要的空驳船数量
        pool1=pool1[:needNumber]
        print("pool1=", pool1)
        print("ways=", ways)

        tii = 0  # 记录是第几次得到了临时最后解
        bbestTempBestloss = float("inf")  # 临时记录最后解
        bbestTempBestj = []  # 临时记录最后解
        bbestTempBestway = []  # 临时记录最后解
        bbestTempBestpool = []  # 临时记录最后解

        BestJ = []  # 记录单拖船不重用的最优解，记录最后的最优解
        BestWay = []  # 记录单拖船不重用的最优解，记录最后的最优解
        BestLoss = float('inf')  # 记录单拖船不重用的最优解，记录最后的最优解
        BestPool = []  # 记录单拖船不重用的最优解，记录最后的最优解

        bestj = []  # 临时存储一下单拖船不重用整体最优解
        bestway = []  # 临时存储一下单拖船不重用整体最优解
        bestloss = float('inf')  # 临时存储一下单拖船不重用整体最优解
        bestpool = []  # 临时存储一下单拖船不重用整体最优解

        # 单拖船不重用
        if needNumber<=maxcap:
            tugNum = 1
            for j in range(len(ways)):  # 选择拖船
                bestJ = 0  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                bestWay = []  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                bestLossE2 = float('inf')  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                bestPool = []  # 因为要调用递归函数，所以在这里初始化这四个全局变量

                comNum[i - orderNumberF * 2][j][0] = needNumber

                for k in range(len(ways[j]) + 1):  # 选择终点插入位置
                    if haveAny1(i,ways[j]):
                        if k!=len(ways[j]):
                            k+=1
                            continue
                    if k==len(ways[j]):
                        pass
                    else:
                        if ways[j][k] < 2*orderNumberF:
                            if k%2==0:
                                if orderF[int((ways[j][k])/2),5]<orderE1[i-2*orderNumberF,7]:
                                    continue
                                else:
                                    pass
                            else:
                                if orderF[int((ways[j][k])/2), 7] < orderE1[i - 2 * orderNumberF, 7]:
                                    continue
                                else:
                                    pass
                        elif ways[j][k]<2*orderNumberF+orderNumberE1:
                            if orderE1[ways[j][k]-2*orderNumberF, 7] < orderE1[i - 2 * orderNumberF, 7]:
                                continue
                            else:
                                pass
                        else:
                            if orderE2[ways[j][k] - 2 * orderNumberF-orderNumberE1, 9] < orderE1[i - 2 * orderNumberF, 7]:
                                continue
                            else:
                                pass
                    tempSolution = clone(ways[j])
                    # print("ways[j]=", ways[j])
                    # print("tempSolution=", tempSolution)
                    tempSolution.insert(k, i)  # 在此位置插入终点
                    # print("tempSolution=", tempSolution)
                    needPoolAll = list(itertools.combinations(pool1, needNumber))  # 从pool1中取出needNumber个数
                    for p in needPoolAll:
                        # print("p=", p)
                        needPools = list(sorted(p, key=lambda i: orderE2[
                            i - 2 * orderNumberF - orderNumberE1, 9]))
                        tempBracket = []
                        tempBracket.append(needPools)
                        needPools = tempBracket

                        for needPool in needPools:
                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                if haveAny2(i, tempSolution):
                                    if pos != k:
                                        pos+=1
                                        continue
                                if pos == k:
                                    pass
                                else:
                                    if tempSolution[pos] < 2 * orderNumberF:
                                        if tempSolution[pos] % 2 == 0:
                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                continue
                                            else:
                                                # print(pos)
                                                pass
                                        else:
                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                continue
                                            else:
                                                pass
                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < orderE2[
                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                            continue
                                        else:
                                            pass
                                    else:
                                        if orderE2[tempSolution[pos] - 2 * orderNumberF - orderNumberE1, 9] < orderE2[
                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                            continue
                                        else:
                                            pass
                                tempNeedPool = clone(needPool)
                                tempTempSolution = clone(tempSolution)
                                tempK = k
                                tempTempSolution.insert(pos, tempNeedPool[0])  # 插入pool中的第一个元素
                                # print("tempTempSolution=",tempTempSolution)
                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                tempK += 1
                                if tempNeedPool != []:
                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, j, needPool)
                                else:
                                    temp = []
                                    for nub in range(courierNumber):
                                        temp.append([])
                                    temp[j] = tempTempSolution
                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                    if (tempLoss < bestLossE2):
                                        # print("bestlossE2=",bestLossE2)
                                        # print("tempLoss=", tempLoss)

                                        bestLossE2 = tempLoss
                                        bestJ = j
                                        bestWay = clone(tempTempSolution)
                                        bestPool = clone(needPool)
                                        # print("bestLossE2=", bestLossE2)
                                        # print("bestPool=", bestPool)
                                        # print("bestWay=", bestWay)
                                        # print("bestJ=", bestJ)

                tempsolution = clone(ways)
                tempsolution[bestJ] = bestWay
                # print("bestWay=",bestWay)
                if (j == 0):
                    # print("j=",j)
                    bestloss = lossFunction(tempsolution, startTime, printf=False)
                    # print("bestloss=", bestloss)
                    bestj = bestJ
                    bestway = bestWay
                    bestpool = bestPool
                else:
                    # print("j=", j)
                    tempLoss = lossFunction(tempsolution, startTime, printf=False)
                    # print("tempLoss=", tempLoss)
                    if (tempLoss < bestloss):
                        bestloss = tempLoss
                        bestj = bestJ
                        bestway = bestWay
                        bestpool = bestPool

            BestLoss = bestloss  # 记录单拖船不重用的最优解，记录最后的最优解
            BestJ.append(bestj)  # 记录单拖船不重用的最优解，记录最后的最优解
            BestWay.append(bestway)  # 记录单拖船不重用的最优解，记录最后的最优解
            BestPool.append(bestpool)  # 记录单拖船不重用的最优解，记录最后的最优解

        # 单拖船重用+两拖船不重用
        elif needNumber<=2*maxcap:
            #单拖船重用
            tugNum=1
            bestTempBestloss = float("inf")  # 记录单拖船重用的最优解
            bestTempBestj = []  # 记录单拖船重用的最优解
            bestTempBestway = []  # 记录单拖船重用的最优解
            bestTempBestpool = []  # 记录单拖船重用的最优解

            ti = 0  # 记录是第几次得到tempBestj、tempBestway、tempBestloss、tempBestpool了

            for j in range(len(ways)):  # 选择拖船
                # print("j=",j)
                for needN in range(needNumber):
                    numberTug = needNumber  # numberTug是一个全局变量
                    nN = needN + 1  # bNum是第一次访问拉空驳船的数量
                    # print("bNum=",bNum)
                    numberTug -= nN  # numberTug是第二次访问拉空驳船的数量
                    if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (nN == 1) or (nN == 2):  # numberTug=0意味着第二次访问拉的数量为0，相当于只考虑了访问一次
                        continue

                    for times in range(maxTravelTime):
                        if times == 0:  # 考虑第一次访问的情况

                            tempTempBestloss = 0#临时存储第一次访问与第二次访问的解
                            tempTempBestj = []#临时存储第一次访问与第二次访问的解
                            tempTempBestway = []#临时存储第一次访问与第二次访问的解
                            tempTempBestpool = []#临时存储第一次访问与第二次访问的解

                            comNum[i - orderNumberF * 2][j][times] = numberTug

                            bestJ = 0  # 临时存储一下局部最优解
                            bestWay = []  # 临时存储一下局部最优解
                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                            bestPool = []  # 临时存储一下局部最优解

                            tempPool = []  # 用于衔接两次分配任务

                            for k in range(len(ways[j]) + 1):  # 选择终点插入位置
                                if haveAny1(i, ways[j]):
                                    if k != len(ways[j]):
                                        k += 1
                                        continue
                                if k == len(ways[j]):
                                    pass
                                else:
                                    if ways[j][k] < 2 * orderNumberF:
                                        if k % 2 == 0:
                                            if orderF[int((ways[j][k]) / 2), 5] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderF[int((ways[j][k]) / 2), 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                    elif ways[j][k] < 2 * orderNumberF + orderNumberE1:
                                        if orderE1[ways[j][k] - 2 * orderNumberF, 7] < orderE1[
                                            i - 2 * orderNumberF, 7]:
                                            continue
                                        else:
                                            pass
                                    else:
                                        if orderE2[ways[j][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                            i - 2 * orderNumberF, 7]:
                                            continue
                                        else:
                                            pass

                                tempSolution = clone(ways[j])
                                # print("ways[j]=", ways[j])
                                # print("tempSolution=", tempSolution)
                                tempSolution.insert(k, i)  # 在此位置插入终点
                                # print("tempSolution=", tempSolution)
                                # print("bNum=", bNum)
                                needPoolAll = list(
                                    itertools.combinations(pool1, numberTug))  # 从pool1中取出needNumber个数
                                # print("needPoolAll=", needPoolAll)
                                for p in needPoolAll:
                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                    tempBracket = []
                                    tempBracket.append(needPools)
                                    needPools = tempBracket
                                    for needPool in needPools:
                                        # print("needPool=", needPool)
                                        for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                            if haveAny2(i, tempSolution):
                                                if pos != k:
                                                    pos += 1
                                                    continue
                                            if pos == k:
                                                pass
                                            else:
                                                if tempSolution[pos] < 2 * orderNumberF:
                                                    if tempSolution[pos] % 2 == 0:
                                                        if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            # print(pos)
                                                            pass
                                                    else:
                                                        if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                    if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < orderE2[
                                                        needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                        continue
                                                    else:
                                                        pass
                                                else:
                                                    if orderE2[
                                                        tempSolution[pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                            orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                        continue
                                                    else:
                                                        pass

                                            tempNeedPool = clone(needPool)
                                            tempTempSolution = clone(tempSolution)
                                            tempK = k
                                            tempTempSolution.insert(pos, tempNeedPool[
                                                0])  # 插入pool中的第一个元素
                                            # print("tempTempSolution=",tempTempSolution)
                                            tempNeedPool.remove(
                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                            tempK += 1
                                            if tempNeedPool != []:
                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                          pos, j,
                                                          needPool)
                                            else:
                                                temp = []
                                                for nub in range(courierNumber):
                                                    temp.append([])
                                                temp[j] = tempTempSolution
                                                tempLoss = lossFunction(temp, startTime,
                                                                        printf=False)

                                                if (tempLoss < bestLossE2):
                                                    # print("bestlossE2=",bestLossE2)
                                                    # print("tempLoss=", tempLoss)

                                                    bestLossE2 = tempLoss
                                                    bestJ = j
                                                    bestWay = clone(tempTempSolution)
                                                    bestPool = clone(needPool)

                            tempTempBestj.append(bestJ)
                            tempTempBestway.append(bestWay)
                            tempTempBestpool.append(bestPool)

                            tempPool = clone(pool1)
                            for point in bestPool:
                                tempPoint = point
                                tempPool.remove(tempPoint)

                            continue

                        if times == 1:  # 考虑第二次访问的情况

                            comNum[i - orderNumberF * 2][j][times] = nN

                            bestJ = 0  # 临时存储一下局部最优解
                            bestWay = []  # 临时存储一下局部最优解
                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                            bestPool = []  # 临时存储一下局部最优解

                            for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                # print("k=", k)
                                couldPos=tempTempBestway[0].index(i)
                                if k<=couldPos:
                                    continue
                                tempSolution = clone(tempTempBestway[0])
                                # print("ways[t]=", ways[t])
                                # print("tempSolution=", tempSolution)
                                tempSolution.insert(k, i)  # 在此位置插入终点
                                # print("tempSolution=", tempSolution)
                                # print("numberTug=", numberTug)
                                needPoolAll = list(itertools.combinations(tempPool, nN))  # 从pool1中取出needNumber个数
                                # print("needPoolAll=", needPoolAll)
                                for p in needPoolAll:
                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                    tempBracket = []
                                    tempBracket.append(needPools)
                                    needPools = tempBracket

                                    for needPool in needPools:
                                        # print("needPool=", needPool)
                                        for pos in range(couldPos+1, k + 1):  # pos是pool中第一个元素要插入的位置
                                            tempNeedPool = clone(needPool)
                                            tempTempSolution = clone(tempSolution)
                                            tempK = k
                                            tempTempSolution.insert(pos, tempNeedPool[0])  # 插入pool中的第一个元素
                                            # print("tempTempSolution=",tempTempSolution)
                                            tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                            tempK += 1
                                            if tempNeedPool != []:
                                                cirInsert(tempK, tempTempSolution, tempNeedPool, pos, j,
                                                          needPool)
                                            else:
                                                temp = []
                                                for nub in range(courierNumber):
                                                    temp.append([])
                                                temp[j] = tempTempSolution
                                                tempLoss = lossFunction(temp, startTime, printf=False)
                                                if (tempLoss < bestLossE2):
                                                    # print("bestlossE2=",bestLossE2)
                                                    # print("tempLoss=", tempLoss)

                                                    bestLossE2 = tempLoss
                                                    bestJ = j
                                                    bestWay = clone(tempTempSolution)
                                                    bestPool = clone(needPool)

                            tempsolution = clone(ways)
                            tempsolution[bestJ] = bestWay

                            comNum[i - orderNumberF * 2][j][0] = numberTug
                            comNum[i - orderNumberF * 2][j][1] = nN

                            tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                            tempTempBestj.append(bestJ)
                            tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                            tempTempBestpool.append(bestPool)

                            if ti == 0:
                                bestTempBestloss = tempTempBestloss
                                bestTempBestj = tempTempBestj
                                bestTempBestway = tempTempBestway
                                bestTempBestpool = tempTempBestpool
                                ti += 1
                            else:
                                if bestTempBestloss > tempTempBestloss:
                                    bestTempBestloss = tempTempBestloss
                                    bestTempBestj = tempTempBestj
                                    bestTempBestway = tempTempBestway
                                    bestTempBestpool = tempTempBestpool
                                ti += 1

            if bestTempBestloss < BestLoss:
                BestLoss = bestTempBestloss
                BestJ = bestTempBestj
                BestWay = bestTempBestway
                BestPool = bestTempBestpool

            #两拖船不重用
            bTempBestloss = float("inf")  # 两拖船不重用的最优解
            bTempBestj = []  # 两拖船不重用的最优解
            bTempBestway = []  # 两拖船不重用的最优解
            bTempBestpool = []  # 两拖船不重用的最优解
            tugNum=2
            ti=0
            tugAll = list(itertools.combinations(tugs, tugNum))
            # print("tugAll=",tugAll)
            for n in tugAll:
                tug = list(itertools.permutations(n))
                # print("tug=",tug)
                for tu in tug:
                    # print("tu=",tu)
                    # print("needNumber=",needNumber)
                    for bargeNum in range(needNumber):
                        # print("bargeNum=",bargeNum)
                        numberTug = needNumber  # numberTug是一个全局变量
                        bNum = bargeNum + 1  # bNum是第一艘拖船拉空驳船的数量
                        # print("bNum=",bNum)
                        numberTug -= bNum  # numberTug是现在还有多少空驳船需要拉
                        # print("numberTug=",numberTug)

                        if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (bNum == 1) or (bNum == 2):
                            # print("break")
                            continue

                        for t in tu:
                            first = tu[0]
                            last = tu[-1]
                            if t == first:
                                comNum[i - orderNumberF * 2][t][0] = bNum

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                tempPool = []  # 用于用于衔接两辆拖船

                                tempBestj = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestway = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestloss = 0  # 用于临时记录拖船的单拖船最优方案
                                tempBestpool = []  # 用于临时记录拖船的单拖船最优方案

                                # 不考虑重用
                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    # print("k=", k)
                                    tempSolution = clone(ways[t])
                                    # print("ways[j]=", ways[j])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("bNum=", bNum)
                                    needPoolAll = list(
                                        itertools.combinations(pool1, bNum))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket
                                        # if bNum==8:
                                        #     print(needPools)
                                        # if needPools==[[31,32,33,34,35,36,37,38]]:
                                        #     print("right here!")
                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)
                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = bNum

                                tempBestj.append(bestJ)  # t拖船的单拖船最优方案
                                tempBestway.append(bestWay)  # t拖船的单拖船最优方案
                                tempBestpool.append(bestPool)  # t拖船的单拖船最优方案

                                tempPool = clone(pool1)
                                for tim in range(len(tempBestj)):
                                    for point in tempBestpool[tim]:
                                        tempPoint = point
                                        tempPool.remove(tempPoint)

                                continue
                            elif t == last:
                                comNum[i - orderNumberF * 2][t][0] = numberTug
                                # print("comNum=", comNum)

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    # print("k=", k)
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[
                                                ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                    orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    tempSolution = clone(ways[t])
                                    # print("ways[t]=", ways[t])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("numberTug=", numberTug)
                                    needPoolAll = list(
                                        itertools.combinations(tempPool,
                                                               numberTug))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket

                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)

                                for timm in range(len(tempBestj)):
                                    tempsolution[tempBestj[timm]] = tempBestway[timm]

                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = numberTug

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                tempBestj.append(bestJ)
                                tempBestway.append(bestWay)
                                tempBestpool.append(bestPool)

                                if ti==0:
                                    bTempBestloss = tempBestloss  # 两拖船不重用的最优解
                                    bTempBestj = tempBestj  # 两拖船不重用的最优解
                                    bTempBestway = tempBestway  # 两拖船不重用的最优解
                                    bTempBestpool = tempBestpool  # 两拖船不重用的最优解
                                    ti+=1
                                elif ti>=1:
                                    if tempBestloss<bTempBestloss:
                                        bTempBestloss = tempBestloss  # 两拖船不重用的最优解
                                        bTempBestj = tempBestj  # 两拖船不重用的最优解
                                        bTempBestway = tempBestway  # 两拖船不重用的最优解
                                        bTempBestpool = tempBestpool  # 两拖船不重用的最优解
                                    ti+=1
            if bTempBestloss < BestLoss:
                BestLoss = bTempBestloss
                BestJ = bTempBestj
                BestWay = bTempBestway
                BestPool = bTempBestpool

        # 双拖船重用
        elif needNumber<=3*maxcap:
            tugNum = 2

            numberTug = 0  # 用于记录第二个拖船需要拉多少个驳船
            nTug1 = 0
            nTug2 = 0

            tugAll = list(itertools.combinations(tugs, tugNum))
            # print("tugAll=",tugAll)
            for n in tugAll:
                tug = list(itertools.permutations(n))
                # print("tug=",tug)
                for tu in tug:
                    # print("tu=",tu)
                    # print("needNumber=",needNumber)
                    for bargeNum in range(needNumber):
                        # print("bargeNum=",bargeNum)
                        numberTug = needNumber  # numberTug是一个全局变量
                        bNum = bargeNum + 1  # bNum是第一艘拖船拉空驳船的数量
                        # print("bNum=",bNum)
                        numberTug -= bNum  # numberTug是现在还有多少空驳船需要拉
                        # print("numberTug=",numberTug)

                        if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (bNum == 1) or (bNum == 2):
                            # print("break")
                            continue

                        for t in tu:
                            first = tu[0]
                            last = tu[-1]
                            if t == first:

                                bTempBestloss = 0  # 第一辆拖船的部分最优解
                                bTempBestj = []  # 第一辆拖船的部分最优解
                                bTempBestway = []  # 第一辆拖船的部分最优解
                                bTempBestpool = []  # 第一辆拖船的部分最优解

                                comNum[i - orderNumberF * 2][t][0] = bNum
                                # print("comNum=", comNum)
                                # print("t=",t)

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                tempPool = []  # 用于用于衔接两辆拖船

                                tempBestj = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestway = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestloss = 0  # 用于临时记录拖船的单拖船最优方案
                                tempBestpool = []  # 用于临时记录拖船的单拖船最优方案

                                # 不考虑重用
                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass


                                    # print("k=", k)
                                    tempSolution = clone(ways[t])
                                    # print("ways[j]=", ways[j])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("bNum=", bNum)
                                    needPoolAll = list(
                                        itertools.combinations(pool1, bNum))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket
                                        # if bNum==8:
                                        #     print(needPools)
                                        # if needPools==[[31,32,33,34,35,36,37,38]]:
                                        #     print("right here!")
                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)
                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = bNum

                                tempBestloss = lossFunction(tempsolution, startTime,printf=False)  # t拖船的单拖船最优方案
                                tempBestj.append(bestJ)  # t拖船的单拖船最优方案
                                tempBestway.append(bestWay)  # t拖船的单拖船最优方案
                                tempBestpool.append(bestPool)  # t拖船的单拖船最优方案

                                bTempBestloss = tempBestloss  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                bTempBestj = tempBestj  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                bTempBestway = tempBestway  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                bTempBestpool = tempBestpool  # 把t拖船的第一次得到的单拖船最优方案存在这里面

                                bestTempTempBestloss = float('inf')#记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                bestTempTempBestj = []#记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                bestTempTempBestway = []#记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                bestTempTempBestpool = []#记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较

                                ti = 0#用于记录是第几次得到了tempTempBestloss

                                # 考虑重用
                                for needM in range(bNum):
                                    nM = needM + 1  # 第一辆拖船第一次访问拉的驳船数
                                    nTug1 = bNum
                                    nTug1 -= nM  # 第一辆拖船第二次访问拉的驳船数
                                    if (nTug1 == 0) or (nTug1 == 1) or (nM == 1) :
                                        continue
                                    for timess in range(maxTravelTime):

                                        if timess == 0:  # 考虑第一次访问的情况

                                            tempTempBestloss = 0#临时记录第一辆拖船的重用最优解
                                            tempTempBestj = []#临时记录第一辆拖船的重用最优解
                                            tempTempBestway = []#临时记录第一辆拖船的重用最优解
                                            tempTempBestpool = []#临时记录第一辆拖船的重用最优解

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            comNum[i - orderNumberF * 2][t][timess] =nTug1

                                            tempPooll = []  # 用于衔接两次访问

                                            for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                                # print("k=", k)
                                                if haveAny1(i, ways[t]):
                                                    if k != len(ways[t]):
                                                        k += 1
                                                        continue
                                                if k == len(ways[t]):
                                                    pass
                                                else:
                                                    if ways[t][k] < 2 * orderNumberF:
                                                        if k % 2 == 0:
                                                            if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                            i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[
                                                            ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass

                                                tempSolution = clone(ways[t])
                                                # print("ways[j]=", ways[t])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                needPoolAll = list(
                                                    itertools.combinations(pool1, nTug1))  # 从pool1中取出needNumber个数

                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket
                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            if haveAny2(i, tempSolution):
                                                                if pos != k:
                                                                    pos += 1
                                                                    continue
                                                            if pos == k:
                                                                pass
                                                            else:
                                                                if tempSolution[pos] < 2 * orderNumberF:
                                                                    if tempSolution[pos] % 2 == 0:
                                                                        if orderF[int((tempSolution[pos]) / 2), 5] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            print(pos)
                                                                            pass
                                                                    else:
                                                                        if orderF[int((tempSolution[pos]) / 2), 7] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass
                                                                elif tempSolution[
                                                                    pos] < 2 * orderNumberF + orderNumberE1:
                                                                    if orderE1[
                                                                        tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass
                                                                else:
                                                                    if orderE2[tempSolution[
                                                                                   pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass

                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t, needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime,
                                                                                        printf=False)
                                                                # print("tempLoss=", tempLoss)

                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=", bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)
                                            tempTempBestpool.append(bestPool)

                                            tempPooll = clone(pool1)
                                            for point in bestPool:
                                                tempPoint = point
                                                tempPooll.remove(tempPoint)

                                            continue

                                        if timess == 1:  # 考虑第二次访问的情况

                                            comNum[i - orderNumberF * 2][t][timess] = nM

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                                # print("k=", k)
                                                couldPos = tempTempBestway[0].index(i)
                                                if k <= couldPos:
                                                    continue
                                                tempSolution = clone(tempTempBestway[0])
                                                # print("ways[t]=", ways[t])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                # print("numberTug=", numberTug)
                                                needPoolAll = list(
                                                    itertools.combinations(tempPooll,
                                                                           nM))  # 从pool1中取出needNumber个数
                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket

                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(couldPos+1, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t,
                                                                          needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime,
                                                                                        printf=False)
                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=",bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempsolution = clone(ways)
                                            tempsolution[bestJ] = bestWay

                                            comNum[i - orderNumberF * 2][t][0] = nTug1
                                            comNum[i - orderNumberF * 2][t][1] = nM

                                            tempTempBestloss = lossFunction(tempsolution, startTime,printf=False)
                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                                            tempTempBestpool.append(bestPool)
                                            if ti == 0:
                                                bestTempTempBestloss = tempTempBestloss
                                                bestTempTempBestj = tempTempBestj
                                                bestTempTempBestway = tempTempBestway
                                                bestTempTempBestpool = tempTempBestpool
                                                ti += 1
                                            else:
                                                if bestTempTempBestloss > tempTempBestloss:
                                                    bestTempTempBestloss = tempTempBestloss
                                                    bestTempTempBestj = tempTempBestj
                                                    bestTempTempBestway = tempTempBestway
                                                    bestTempTempBestpool = tempTempBestpool
                                                ti += 1

                                if bestTempTempBestloss < bTempBestloss:
                                    bTempBestloss = bestTempTempBestloss
                                    bTempBestj = bestTempTempBestj  # [a,b]
                                    bTempBestway = bestTempTempBestway  # [[a,b],[c,d]]
                                    bTempBestpool = bestTempTempBestpool  # [[a,b],[c,d]]

                                tempPool = clone(pool1)
                                for tim in range(len(bTempBestj)):
                                    for point in bTempBestpool[tim]:
                                        tempPoint = point
                                        tempPool.remove(tempPoint)

                                continue

                                # 至此第一个拖船的最优方案已经存在了bTempBestloss、bTempBestj、bTempBestway、bTempBestpool里面
                                # 以及供第二辆拖船选择的驳船都已经存放在tempPool里面了

                            elif t == last:  # maxTugNum=2时才会进入这里
                                # print("t=",t)#对于最后一个元素t来说，他只有一种选择

                                # 不考虑重用
                                comNum[i - orderNumberF * 2][t][0] = numberTug
                                # print("comNum=", comNum)

                                tempBestloss = 0  # 用于临时记录第二辆拖船不重用的最优解
                                tempBestj = []   # 用于临时记录第二辆拖船不重用的最优解
                                tempBestway = []   # 用于临时记录第二辆拖船不重用的最优解
                                tempBestpool = []   # 用于临时记录第二辆拖船不重用的最优解

                                tTempBestloss = 0  # 第二辆拖船的最优解
                                tTempBestj = []  # 第二辆拖船的最优解
                                tTempBestway = []  # 第二辆拖船的最优解
                                tTempBestpool = []  # 第二辆拖船的最优解

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    # print("k=", k)
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[
                                                ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                    orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    tempSolution = clone(ways[t])
                                    # print("ways[t]=", ways[t])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("numberTug=", numberTug)
                                    needPoolAll = list(
                                        itertools.combinations(tempPool,
                                                               numberTug))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket

                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)

                                for timm in range(len(bTempBestj)):  # bTempBestj、bTempBestway这些都是第一辆拖船的部分最优解
                                    tempsolution[bTempBestj[timm]] = bTempBestway[timm]

                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = numberTug

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                tempBestj.append(bestJ)
                                tempBestway.append(bestWay)
                                tempBestpool.append(bestPool)

                                tTempBestloss = tempBestloss  # 第二辆拖船的最优解
                                tTempBestj = tempBestj  # 第二辆拖船的最优解
                                tTempBestway = tempBestway  # 第二辆拖船的最优解
                                tTempBestpool = tempBestpool  # 第二辆拖船的最优解

                                bestTempTempBestloss = float("inf")#用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                bestTempTempBestj = []#用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                bestTempTempBestway = []#用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                bestTempTempBestpool = []#用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较

                                ti = 0

                                # 考虑重用
                                for needB in range(numberTug):
                                    nB = needB + 1  # 第一辆拖船第一次访问拉的驳船数
                                    nTug2 = numberTug
                                    nTug2 -= nB  # 第一辆拖船第二次访问拉的驳船数
                                    if (nTug2 == 0) or (nTug2 == 1) or (nB == 1):
                                        continue

                                    for timess in range(maxTravelTime):
                                        if timess == 0:  # 考虑第一次访问的情况

                                            tempTempBestloss = 0#用于临时记录第二辆拖船重用的最优解
                                            tempTempBestj = []#用于临时记录第二辆拖船重用的最优解
                                            tempTempBestway = []#用于临时记录第二辆拖船重用的最优解
                                            tempTempBestpool = []#用于临时记录第二辆拖船重用的最优解

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            comNum[i - orderNumberF * 2][t][timess] = nTug2

                                            ttempPool = []  # 用于衔接两次分配任务

                                            for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                                # print("k=", k)
                                                if haveAny1(i, ways[t]):
                                                    if k != len(ways[t]):
                                                        k += 1
                                                        continue
                                                if k == len(ways[t]):
                                                    pass
                                                else:
                                                    if ways[t][k] < 2 * orderNumberF:
                                                        if k % 2 == 0:
                                                            if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                            i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[
                                                            ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass

                                                tempSolution = clone(ways[t])
                                                # print("ways[j]=", ways[j])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                # print("bNum=", bNum)
                                                needPoolAll = list(
                                                    itertools.combinations(tempPool,
                                                                           nTug2))  # 从pool1中取出needNumber个数
                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket

                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            if haveAny2(i, tempSolution):
                                                                if pos != k:
                                                                    pos += 1
                                                                    continue
                                                            if pos == k:
                                                                pass
                                                            else:
                                                                if tempSolution[pos] < 2 * orderNumberF:
                                                                    if tempSolution[pos] % 2 == 0:
                                                                        if orderF[int((tempSolution[pos]) / 2), 5] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            # print(pos)
                                                                            pass
                                                                    else:
                                                                        if orderF[int((tempSolution[pos]) / 2), 7] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass
                                                                elif tempSolution[
                                                                    pos] < 2 * orderNumberF + orderNumberE1:
                                                                    if orderE1[
                                                                        tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass
                                                                else:
                                                                    if orderE2[tempSolution[
                                                                                   pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass

                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t,
                                                                          needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime,printf=False)

                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=",bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)
                                            tempTempBestpool.append(bestPool)

                                            comNum[i - orderNumberF * 2][t][timess] = nTug2

                                            ttempPool = clone(tempPool)
                                            for point in bestPool:
                                                tempPoint = point
                                                ttempPool.remove(tempPoint)

                                            continue

                                        if timess == 1:  # 考虑第二次访问的情况

                                            comNum[i - orderNumberF * 2][t][timess] = nB

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                                couldPos=tempTempBestway[0].index(i)
                                                if k<=couldPos:
                                                    continue
                                                # print("k=", k)
                                                tempSolution = clone(tempTempBestway[0])
                                                # print("ways[t]=", ways[t])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                # print("numberTug=", numberTug)
                                                needPoolAll = list(
                                                    itertools.combinations(ttempPool,
                                                                           nB))  # 从pool1中取出needNumber个数
                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket

                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(couldPos+1, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t,
                                                                          needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime,
                                                                                        printf=False)
                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=",bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempsolution = clone(ways)
                                            tempsolution[bestJ] = bestWay

                                            comNum[i - orderNumberF * 2][t][0] = nTug2
                                            comNum[i - orderNumberF * 2][t][1] = nB

                                            tempTempBestloss = lossFunction(tempsolution, startTime,printf=False)
                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                                            tempTempBestpool.append(bestPool)

                                            if ti == 0:
                                                bestTempTempBestloss = tempTempBestloss
                                                bestTempTempBestj = tempTempBestj
                                                bestTempTempBestway = tempTempBestway
                                                bestTempTempBestpool = tempTempBestpool
                                                ti += 1
                                            else:
                                                if bestTempTempBestloss > tempTempBestloss:
                                                    bestTempTempBestloss = tempTempBestloss
                                                    bestTempTempBestj = tempTempBestj
                                                    bestTempTempBestway = tempTempBestway
                                                    bestTempTempBestpool = tempTempBestpool
                                                ti += 1

                                if bestTempTempBestloss < tTempBestloss:
                                    tTempBestloss = bestTempTempBestloss
                                    tTempBestj = bestTempTempBestj  # [a,b]
                                    tTempBestway = bestTempTempBestway  # [[a,b],[c,d]]
                                    tTempBestpool = bestTempTempBestpool  # [[a,b],[c,d]]

                                # 至此，第一辆拖船以及第二辆拖船的最优部分解均已经收集完毕

                                for leng in range(len(tTempBestj)):
                                    bTempBestj.append(tTempBestj[leng])
                                    bTempBestway.append(tTempBestway[leng])
                                    bTempBestpool.append(tTempBestpool[leng])

                                tempsolution = clone(ways)

                                for leng in range(len(bTempBestj)):
                                    tempsolution[bTempBestj[leng]] = bTempBestway[leng]

                                tNum = []

                                for ii in range(orderNumberE1):
                                    tNum.append([])
                                    for jj in range(courierNumber):
                                        tNum[ii].append([])
                                        for _ in range(2):
                                            tNum[ii][jj].append(0)

                                comNum[i - orderNumberF * 2] = tNum[i - orderNumberF * 2]

                                tempp = []
                                for to in bTempBestj:
                                    if to in tempp:
                                        # 查找 to 在 bbestTempBestj 中第二次出现的位置
                                        index = find_nth_occurrence(bTempBestj, to, 2)
                                        if index != -1:
                                            comNum[i - orderNumberF * 2][to][1] = len(bTempBestpool[index])
                                    else:
                                        index = bTempBestj.index(to)
                                        comNum[i - orderNumberF * 2][to][0] = len(bTempBestpool[index])
                                    tempp.append(to)

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)

                                if tii == 0:
                                    bbestTempBestloss = tempBestloss
                                    bbestTempBestj = bTempBestj
                                    bbestTempBestway = bTempBestway
                                    bbestTempBestpool = bTempBestpool
                                    tii += 1
                                elif tii != 0:
                                    tii += 1
                                    if bbestTempBestloss >= tempBestloss:
                                        bbestTempBestloss = tempBestloss
                                        bbestTempBestj = bTempBestj
                                        bbestTempBestway = bTempBestway
                                        bbestTempBestpool = bTempBestpool
                if bbestTempBestloss < BestLoss:
                    BestLoss = bbestTempBestloss
                    BestJ = bbestTempBestj
                    BestWay = bbestTempBestway
                    BestPool = bbestTempBestpool

        pool1 = tempPool1
        for chosenone in range(len(BestJ)):
            ways[BestJ[chosenone]] = BestWay[chosenone]
            for point in BestPool[chosenone]:
                tempPoint = point
                pool1.remove(tempPoint)
        tempPool1=pool1
        tNum = []

        for ii in range(orderNumberE1):
            tNum.append([])
            for jj in range(courierNumber):
                tNum[ii].append([])
                for _ in range(2):
                    tNum[ii][jj].append(0)

        comNum[i - orderNumberF * 2]=tNum[i - orderNumberF * 2]

        tempp = []
        for to in BestJ:
            if to in tempp:
                # 查找 to 在 bbestTempBestj 中第二次出现的位置
                index = find_nth_occurrence(BestJ, to, 2)
                if index != -1:
                    comNum[i - orderNumberF * 2][to][1] = len(BestPool[index])
            else:
                index = BestJ.index(to)
                comNum[i - orderNumberF * 2][to][0] = len(BestPool[index])
            tempp.append(to)
    pool1 = []
    pool2 = []
    return ways


################################################


##################F类型订单修复算子######################
# 随机贪婪修复F类型订单的某路径
def repairF1(ways, pool):  # 随机重建算子
    shuffle(pool)
    for i in pool:
        bindex1 = 0  # 路径参数
        bindex2 = 0  # 第一个目标点参数
        bindex3 = 0  # 第二个目标点参数
        index1 = 0  # 存储bestLoss对应的拖船参数
        index2 = 0  # 存储bestLoss对应的F类型订单起点参数
        index3 = 0  # 存储bestLoss对应的F类型订单终点参数
        bestLoss = 0
        bestLossF2 = 0
        if (i % 2 == 0):  # 当对应的是F类型订单起点的时候进入循环
            for j in range(len(ways)):  # 选择拖船
                for k in range(len(ways[j]) + 1):  # 选择起点插入位置
                    tempSolution = clone(ways[j])
                    tempSolution.insert(k, i)  # 在此位置插入起点
                    for l in range(k + 1, len(tempSolution) + 1):  # 选择终点插入位置
                        tempTempSolution = clone(tempSolution)
                        tempTempSolution.insert(l, i + 1)
                        temp = []
                        for nu in range(courierNumber):
                            temp.append([])
                        temp[j] = tempTempSolution
                        tempLoss = lossFunction(temp, startTime, printf=False)
                        if (tempLoss < bestLossF2 or (k == 0 and l == 1)):
                            bestLossF2 = tempLoss
                            index1 = j
                            index2 = k
                            index3 = l
                tempsolution = clone_runoob(ways)
                tempsolution[index1].insert(index2, i)
                tempsolution[index1].insert(index3, i + 1)
                if (j == 0):
                    bestLoss = lossFunction(tempsolution, startTime, printf=False)
                    bindex1 = index1
                    bindex2 = index2
                    bindex3 = index3
                else:
                    tempLoss = lossFunction(tempsolution, startTime, printf=False)
                    if (tempLoss < bestLoss):
                        bestLoss = tempLoss
                        bindex1 = index1
                        bindex2 = index2
                        bindex3 = index3
            ways[bindex1].insert(bindex2, i)
            ways[bindex1].insert(bindex3, i + 1)
    pool = []
    return ways, pool


# 深度贪婪修复F类型订单的某路径
def repairF2(ways, pool):  # 贪婪重建算子
    # 分别在每个拖船路径中找到插入的最优值，记录下来，然后再比较整个的，找到这个的插入位置
    for totaltime in range(len(pool) // 2 - 1):
        bbindex1 = 0
        bbindex2 = 0
        bbindex3 = 0
        bbi = -1
        bblost = 0
        ti = 0
        for i in pool:
            bindex1 = 0  # 路径参数
            bindex2 = 0  # 第一个目标点参数
            bindex3 = 0  # 第二个目标点参数
            index1 = 0  # 存储bestLoss对应的拖船参数
            index2 = 0  # 存储bestLoss对应的F类型订单起点参数
            index3 = 0  # 存储bestLoss对应的F类型订单终点参数
            bestLoss = 0
            bestLossF2 = 0
            if (i % 2 == 0):  # 当对应的是F类型订单起点的时候进入循环
                for j in range(len(ways)):  # 选择拖船
                    for k in range(len(ways[j]) + 1):  # 选择起点插入位置
                        tempSolution = clone(ways[j])
                        tempSolution.insert(k, i)  # 在此位置插入起点
                        for l in range(k + 1, len(tempSolution) + 1):  # 选择终点插入位置
                            tempTempSolution = clone(tempSolution)
                            tempTempSolution.insert(l, i + 1)
                            temp = []
                            for nu in range(courierNumber):
                                temp.append([])
                            temp[j] = tempTempSolution
                            tempLoss = lossFunction(temp, startTime, printf=False)
                            if (tempLoss < bestLossF2 or (k == 0 and l == 1)):
                                bestLossF2 = tempLoss
                                index1 = j
                                index2 = k
                                index3 = l
                    tempsolution = clone_runoob(ways)
                    tempsolution[index1].insert(index2, i)
                    tempsolution[index1].insert(index3, i + 1)
                    if (j == 0):
                        bestLoss = lossFunction(tempsolution, startTime, printf=False)
                        bindex1 = index1
                        bindex2 = index2
                        bindex3 = index3
                    else:
                        tempLoss = lossFunction(tempsolution, startTime, printf=False)
                        if (tempLoss < bestLoss):
                            bestLoss = tempLoss
                            bindex1 = index1
                            bindex2 = index2
                            bindex3 = index3
                if ti == 0:  # 选出提升最大的loss优先插入
                    bblost = bestLoss
                    bbindex1 = bindex1
                    bbindex2 = bindex2
                    bbindex3 = bindex3
                    bbi = i
                    ti += 1
                else:
                    if bestLoss < bblost:
                        bblost = bestLoss
                        bbindex1 = bindex1
                        bbindex2 = bindex2
                        bbindex3 = bindex3
                        bbi = i
                    ti += 1
        if (pool != []) and (bbi != -1):
            ways[bbindex1].insert(bbindex2, bbi)
            ways[bbindex1].insert(bbindex3, bbi + 1)
            pool.remove(bbi)
            pool.remove(bbi + 1)

    for i in pool:
        bindex1 = 0  # 路径参数
        bindex2 = 0  # 第一个目标点参数
        bindex3 = 0  # 第二个目标点参数
        index1 = 0  # 存储bestLoss对应的拖船参数
        index2 = 0  # 存储bestLoss对应的F类型订单起点参数
        index3 = 0  # 存储bestLoss对应的F类型订单终点参数
        bestLoss = 0
        bestLossF2 = 0
        if (i % 2 == 0):  # 当对应的是F类型订单起点的时候进入循环
            for j in range(len(ways)):  # 选择拖船
                for k in range(len(ways[j]) + 1):  # 选择起点插入位置
                    tempSolution = clone(ways[j])
                    tempSolution.insert(k, i)  # 在此位置插入起点
                    for l in range(k + 1, len(tempSolution) + 1):  # 选择终点插入位置
                        tempTempSolution = clone(tempSolution)
                        tempTempSolution.insert(l, i + 1)
                        temp = []
                        for nu in range(courierNumber):
                            temp.append([])
                        temp[j] = tempTempSolution
                        tempLoss = lossFunction(temp, startTime, printf=False)
                        if (tempLoss < bestLossF2 or (k == 0 and l == 1)):
                            bestLossF2 = tempLoss
                            index1 = j
                            index2 = k
                            index3 = l
                tempsolution = clone_runoob(ways)
                tempsolution[index1].insert(index2, i)
                tempsolution[index1].insert(index3, i + 1)
                if (j == 0):
                    bestLoss = lossFunction(tempsolution, startTime, printf=False)
                    bindex1 = index1
                    bindex2 = index2
                    bindex3 = index3
                else:
                    tempLoss = lossFunction(tempsolution, startTime, printf=False)
                    if (tempLoss < bestLoss):
                        bestLoss = tempLoss
                        bindex1 = index1
                        bindex2 = index2
                        bindex3 = index3
            ways[bindex1].insert(bindex2, i)
            ways[bindex1].insert(bindex3, i + 1)

    pool = []
    return ways, pool


# 遗憾修复F类型订单的某路径
def repairF3(ways, pool):  # 贪婪重建算子
    # 分别在每个拖船路径中找到插入的最优值，记录下来，然后再比较整个的，找到这个的插入位置
    for totaltime in range(len(pool) // 2 - 1):
        bbindex1 = 0
        bbindex2 = 0
        bbindex3 = 0
        bbi = -1
        bblost = 0
        ti = 0
        for i in pool:
            bindex1 = 0  # 路径参数
            bindex2 = 0  # 第一个目标点参数
            bindex3 = 0  # 第二个目标点参数

            index1 = 0  # 存储bestLoss对应的拖船参数
            index2 = 0  # 存储bestLoss对应的F类型订单起点参数
            index3 = 0  # 存储bestLoss对应的F类型订单终点参数
            bestLoss = 0
            bestLoss2 = 0

            bestLossF2 = 0
            if (i % 2 == 0):  # 当对应的是F类型订单起点的时候进入循环
                for j in range(len(ways)):  # 选择拖船
                    for k in range(len(ways[j]) + 1):  # 选择起点插入位置
                        tempSolution = clone(ways[j])
                        tempSolution.insert(k, i)  # 在此位置插入起点
                        for l in range(k + 1, len(tempSolution) + 1):  # 选择终点插入位置
                            tempTempSolution = clone(tempSolution)
                            tempTempSolution.insert(l, i + 1)
                            temp = []
                            for nu in range(courierNumber):
                                temp.append([])
                            temp[j] = tempTempSolution
                            tempLoss = lossFunction(temp, startTime, printf=False)
                            if (tempLoss < bestLossF2 or (k == 0 and l == 1)):
                                bestLossF2 = tempLoss
                                index1 = j
                                index2 = k
                                index3 = l
                    tempsolution = clone_runoob(ways)
                    tempsolution[index1].insert(index2, i)
                    tempsolution[index1].insert(index3, i + 1)
                    if (j == 0):
                        bestLoss = lossFunction(tempsolution, startTime, printf=False)
                        bindex1 = index1
                        bindex2 = index2
                        bindex3 = index3
                    elif (j == 1):
                        tempLoss = lossFunction(tempsolution, startTime, printf=False)
                        if (tempLoss < bestLoss):
                            bestLoss2 = bestLoss

                            bestLoss = tempLoss
                            bindex1 = index1
                            bindex2 = index2
                            bindex3 = index3
                        else:
                            bestLoss2 = tempLoss
                    else:
                        tempLoss = lossFunction(tempsolution, startTime, printf=False)
                        if (tempLoss < bestLoss2) and (tempLoss > bestLoss):
                            bestLoss2 = tempLoss
                        elif (tempLoss < bestLoss):
                            bestLoss2 = bestLoss

                            bestLoss = tempLoss
                            bindex1 = index1
                            bindex2 = index2
                            bindex3 = index3
                bestLoss = bestLoss2 - bestLoss
                if ti == 0:  # 选出后悔值最大的优先插入
                    bblost = bestLoss
                    bbindex1 = bindex1
                    bbindex2 = bindex2
                    bbindex3 = bindex3
                    bbi = i
                    ti += 1
                else:
                    if bestLoss < bblost:
                        bblost = bestLoss
                        bbindex1 = bindex1
                        bbindex2 = bindex2
                        bbindex3 = bindex3
                        bbi = i
                    ti += 1
        if (pool != []) and (bbi != -1):
            ways[bbindex1].insert(bbindex2, bbi)
            ways[bbindex1].insert(bbindex3, bbi + 1)
            pool.remove(bbi)
            pool.remove(bbi + 1)

    for i in pool:
        bindex1 = 0  # 路径参数
        bindex2 = 0  # 第一个目标点参数
        bindex3 = 0  # 第二个目标点参数
        index1 = 0  # 存储bestLoss对应的拖船参数
        index2 = 0  # 存储bestLoss对应的F类型订单起点参数
        index3 = 0  # 存储bestLoss对应的F类型订单终点参数
        bestLoss = 0
        bestLossF2 = 0
        if (i % 2 == 0):  # 当对应的是F类型订单起点的时候进入循环
            for j in range(len(ways)):  # 选择拖船
                for k in range(len(ways[j]) + 1):  # 选择起点插入位置
                    tempSolution = clone(ways[j])
                    tempSolution.insert(k, i)  # 在此位置插入起点
                    for l in range(k + 1, len(tempSolution) + 1):  # 选择终点插入位置
                        tempTempSolution = clone(tempSolution)
                        tempTempSolution.insert(l, i + 1)
                        temp = []
                        for nu in range(courierNumber):
                            temp.append([])
                        temp[j] = tempTempSolution
                        tempLoss = lossFunction(temp, startTime, printf=False)
                        if (tempLoss < bestLossF2 or (k == 0 and l == 1)):
                            bestLossF2 = tempLoss
                            index1 = j
                            index2 = k
                            index3 = l
                tempsolution = clone_runoob(ways)
                tempsolution[index1].insert(index2, i)
                tempsolution[index1].insert(index3, i + 1)
                if (j == 0):
                    bestLoss = lossFunction(tempsolution, startTime, printf=False)
                    bindex1 = index1
                    bindex2 = index2
                    bindex3 = index3
                else:
                    tempLoss = lossFunction(tempsolution, startTime, printf=False)
                    if (tempLoss < bestLoss):
                        bestLoss = tempLoss
                        bindex1 = index1
                        bindex2 = index2
                        bindex3 = index3
            ways[bindex1].insert(bindex2, i)
            ways[bindex1].insert(bindex3, i + 1)

    pool = []
    return ways, pool


######################################################


##################F类型订单摧毁算子######################
# 随机摧毁F类型订单的某路径
def destroyF1(ways, step):  # 随机摧毁算子
    pool = []
    for i in range(step):
        index1 = random.randint(0, len(ways) - 1)  # 在0到4双闭区间生成一个整数
        while (len(ways[index1]) < 1):  # 看看有没有那种空解，有的话重新生成一个
            index1 = random.randint(0, len(ways) - 1)  # 在0到4双闭区间生成一个整数
        index2 = random.randint(0, len(ways[index1]) - 1)  # 在0到len(ways[index1]) - 1双闭区间生成一个整数
        x = ways[index1][index2]

        while (x >= orderNumberF * 2):
            index1 = random.randint(0, len(ways) - 1)  # 在0到4双闭区间生成一个整数
            while (len(ways[index1]) < 1):  # 看看有没有那种空解，有的话重新生成一个
                index1 = random.randint(0, len(ways) - 1)  # 在0到4双闭区间生成一个整数
            index2 = random.randint(0, len(ways[index1]) - 1)  # 在0到len(ways[index1]) - 1双闭区间生成一个整数
            x = ways[index1][index2]

        if x <= orderNumberF * 2 - 1:
            if (x % 2 == 0):  # 判断是不是起点
                y = x + 1
            else:
                y = x - 1
                x, y = y, x
            ways[index1].remove(x)
            ways[index1].remove(y)
            pool.append(x)
            pool.append(y)
            # print("pool=",pool)
    return ways, pool


# 贪婪摧毁F类型订单的某路径
def destroyF2(ways, step):  # 较差摧毁算子
    pool = []
    for _ in range(step):
        worstLoss = 0
        worstc = 0
        worsto = -1
        # print("len(ways)=",len(ways))
        for i in range(len(ways)):
            temp = []
            for nu in range(courierNumber):
                temp.append([])
            temp[i] = ways[i]
            # print("temp=", temp)
            # print("ways[i]=",ways[i])
            nowLoss = lossFunction(temp, startTime, printf=False)  # startTime是全局变量 #相当于是按顺序把拖船的任务顺序放进去
            # print("nowloss=",nowLoss)
            # print("worstLoss=", worstLoss)
            if (nowLoss > worstLoss):
                worstLoss = nowLoss
                worstc = i
                # print("worstc=",worstc)#把loss最大拖船丢进tempp
        tempp = []
        for num in range(courierNumber):
            tempp.append([])
        tempp[worstc] = ways[worstc]
        bestLoss = lossFunction(tempp, startTime, printf=False)  # loss最大的拖船对应的loss，称之为bestLoss

        for k in range(len(ways[worstc])):
            # print("len(ways[worstc])=",len(ways[worstc]))
            if ways[worstc][k] >= orderNumberF * 2:
                continue
            else:
                if (ways[worstc][k] % 2 == 0):  # 如果是商家
                    # print("ways[worstc][k]=", ways[worstc][k])
                    tempSolution = clone(ways[worstc])  # 把loss最大的拖船的路径复制到tempSolution
                    tempSolution.remove(ways[worstc][k])
                    tempSolution.remove(ways[worstc][k] + 1)
                    temp = []
                    for numm in range(courierNumber):
                        temp.append([])
                    temp[worstc] = tempSolution
                    tempLoss = lossFunction(temp, startTime, printf=False)
                    if (tempLoss < bestLoss):
                        bestLoss = tempLoss
                        worsto = k
                        # 找到最差的解之后
        if worsto != -1:
            x = ways[worstc][worsto]
            y = x + 1
            ways[worstc].remove(x), ways[worstc].remove(y)
            pool.append(x), pool.append(y)
    return ways, pool


######################################################


##################E类型订单修复算子######################
# 自适应规模-随机贪婪修复E类型订单的某路径
def repairE1(ways, pool1, pool2):  # 贪婪重建算子
    global comNum  # 用于记录每个拖船在完成相应的E类型时第一次（如果有第二次的话）拉了几个空驳船

    global tugs  # 一会儿用它结合lossFunction操控拖船

    global bestJ  # 调用递归函数时要用
    global bestWay  # 调用递归函数时要用
    global bestLossE2  # 调用递归函数时要用
    global bestPool  # 调用递归函数时要用

    global numberTug  # 用于记录tugNum等于1时第二次访问拖船需要拉多少个驳船，以及tugNum等于二时第二艘拖船需要拉多少个驳船
    global nTug1  # 用于记录tugNum等于2时第一个拖船第二次访问拖船需要拉几个驳船
    global nTug2  # 用于记录tugNum等于2时第二个拖船第二次访问拖船需要拉几个驳船

    global maxTugNum  # 最多允许几个拖船共同完成一个E类型订单任务（默认为2
    global maxTravelTime  # 最多允许一个拖船访问同一个任务点几次（默认为2

    BestJJ=[]
    BestWayy=[]
    BestLosss=float("inf")
    BestPooll=[]
    BestI=0
    timer=0
    tempPool1 = pool1

    for i in pool2:
        # print("i=", i)
        # print("pool1=", pool1)
        # print("pool2=", pool2)
        # print("ways=", ways)
        needNumber = int(orderE1[i - orderNumberF * 2][8])  # 获取E类型订单需要的空驳船数量
        pool1 = pool1[:needNumber]

        tii = 0  # 记录是第几次得到了临时最后解
        bbestTempBestloss = float("inf")  # 临时记录最后解
        bbestTempBestj = []  # 临时记录最后解
        bbestTempBestway = []  # 临时记录最后解
        bbestTempBestpool = []  # 临时记录最后解

        BestJ = []  # 记录单拖船不重用的最优解，记录最后的最优解
        BestWay = []  # 记录单拖船不重用的最优解，记录最后的最优解
        BestLoss = float('inf')  # 记录单拖船不重用的最优解，记录最后的最优解
        BestPool = []  # 记录单拖船不重用的最优解，记录最后的最优解

        bestj = []  # 临时存储一下单拖船不重用整体最优解
        bestway = []  # 临时存储一下单拖船不重用整体最优解
        bestloss = float('inf')  # 临时存储一下单拖船不重用整体最优解
        bestpool = []  # 临时存储一下单拖船不重用整体最优解

        # 单拖船不重用
        if needNumber <= maxcap:
            tugNum = 1
            for j in range(len(ways)):  # 选择拖船
                bestJ = 0  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                bestWay = []  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                bestLossE2 = float('inf')  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                bestPool = []  # 因为要调用递归函数，所以在这里初始化这四个全局变量

                comNum[i - orderNumberF * 2][j][0] = needNumber

                for k in range(len(ways[j]) + 1):  # 选择终点插入位置
                    if haveAny1(i, ways[j]):
                        if k != len(ways[j]):
                            k += 1
                            continue
                    if k == len(ways[j]):
                        pass
                    else:
                        if ways[j][k] < 2 * orderNumberF:
                            if k % 2 == 0:
                                if orderF[int((ways[j][k]) / 2), 5] < orderE1[i - 2 * orderNumberF, 7]:
                                    continue
                                else:
                                    pass
                            else:
                                if orderF[int((ways[j][k]) / 2), 7] < orderE1[i - 2 * orderNumberF, 7]:
                                    continue
                                else:
                                    pass
                        elif ways[j][k] < 2 * orderNumberF + orderNumberE1:
                            if orderE1[ways[j][k] - 2 * orderNumberF, 7] < orderE1[i - 2 * orderNumberF, 7]:
                                continue
                            else:
                                pass
                        else:
                            if orderE2[ways[j][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                i - 2 * orderNumberF, 7]:
                                continue
                            else:
                                pass
                    tempSolution = clone(ways[j])
                    # print("ways[j]=", ways[j])
                    # print("tempSolution=", tempSolution)
                    tempSolution.insert(k, i)  # 在此位置插入终点
                    # print("tempSolution=", tempSolution)
                    needPoolAll = list(itertools.combinations(pool1, needNumber))  # 从pool1中取出needNumber个数
                    for p in needPoolAll:
                        # print("p=", p)
                        needPools = list(sorted(p, key=lambda i: orderE2[
                            i - 2 * orderNumberF - orderNumberE1, 9]))
                        tempBracket = []
                        tempBracket.append(needPools)
                        needPools = tempBracket

                        for needPool in needPools:
                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                if haveAny2(i, tempSolution):
                                    if pos != k:
                                        pos += 1
                                        continue
                                if pos == k:
                                    pass
                                else:
                                    if tempSolution[pos] < 2 * orderNumberF:
                                        if tempSolution[pos] % 2 == 0:
                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                continue
                                            else:
                                                # print(pos)
                                                pass
                                        else:
                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                continue
                                            else:
                                                pass
                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < orderE2[
                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                            continue
                                        else:
                                            pass
                                    else:
                                        if orderE2[tempSolution[pos] - 2 * orderNumberF - orderNumberE1, 9] < orderE2[
                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                            continue
                                        else:
                                            pass
                                tempNeedPool = clone(needPool)
                                tempTempSolution = clone(tempSolution)
                                tempK = k
                                tempTempSolution.insert(pos, tempNeedPool[0])  # 插入pool中的第一个元素
                                # print("tempTempSolution=",tempTempSolution)
                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                tempK += 1
                                if tempNeedPool != []:
                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, j, needPool)
                                else:
                                    temp = []
                                    for nub in range(courierNumber):
                                        temp.append([])
                                    temp[j] = tempTempSolution
                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                    if (tempLoss < bestLossE2):
                                        # print("bestlossE2=",bestLossE2)
                                        # print("tempLoss=", tempLoss)

                                        bestLossE2 = tempLoss
                                        bestJ = j
                                        bestWay = clone(tempTempSolution)
                                        bestPool = clone(needPool)
                                        # print("bestLossE2=", bestLossE2)
                                        # print("bestPool=", bestPool)
                                        # print("bestWay=", bestWay)
                                        # print("bestJ=", bestJ)

                tempsolution = clone(ways)
                tempsolution[bestJ] = bestWay
                # print("bestWay=",bestWay)
                if (j == 0):
                    # print("j=",j)
                    bestloss = lossFunction(tempsolution, startTime, printf=False)
                    # print("bestloss=", bestloss)
                    bestj = bestJ
                    bestway = bestWay
                    bestpool = bestPool
                else:
                    # print("j=", j)
                    tempLoss = lossFunction(tempsolution, startTime, printf=False)
                    # print("tempLoss=", tempLoss)
                    if (tempLoss < bestloss):
                        bestloss = tempLoss
                        bestj = bestJ
                        bestway = bestWay
                        bestpool = bestPool

            BestLoss = bestloss  # 记录单拖船不重用的最优解，记录最后的最优解
            BestJ.append(bestj)  # 记录单拖船不重用的最优解，记录最后的最优解
            BestWay.append(bestway)  # 记录单拖船不重用的最优解，记录最后的最优解
            BestPool.append(bestpool)  # 记录单拖船不重用的最优解，记录最后的最优解

        # 单拖船重用+两拖船不重用
        elif needNumber <= 2 * maxcap:
            # 单拖船重用
            tugNum = 1
            bestTempBestloss = float("inf")  # 记录单拖船重用的最优解
            bestTempBestj = []  # 记录单拖船重用的最优解
            bestTempBestway = []  # 记录单拖船重用的最优解
            bestTempBestpool = []  # 记录单拖船重用的最优解

            ti = 0  # 记录是第几次得到tempBestj、tempBestway、tempBestloss、tempBestpool了

            for j in range(len(ways)):  # 选择拖船
                # print("j=",j)
                for needN in range(needNumber):
                    numberTug = needNumber  # numberTug是一个全局变量
                    nN = needN + 1  # bNum是第一次访问拉空驳船的数量
                    # print("bNum=",bNum)
                    numberTug -= nN  # numberTug是第二次访问拉空驳船的数量
                    if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (nN == 1) or (
                            nN == 2):  # numberTug=0意味着第二次访问拉的数量为0，相当于只考虑了访问一次
                        continue

                    for times in range(maxTravelTime):
                        if times == 0:  # 考虑第一次访问的情况

                            tempTempBestloss = 0  # 临时存储第一次访问与第二次访问的解
                            tempTempBestj = []  # 临时存储第一次访问与第二次访问的解
                            tempTempBestway = []  # 临时存储第一次访问与第二次访问的解
                            tempTempBestpool = []  # 临时存储第一次访问与第二次访问的解

                            comNum[i - orderNumberF * 2][j][times] = numberTug

                            bestJ = 0  # 临时存储一下局部最优解
                            bestWay = []  # 临时存储一下局部最优解
                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                            bestPool = []  # 临时存储一下局部最优解

                            tempPool = []  # 用于衔接两次分配任务

                            for k in range(len(ways[j]) + 1):  # 选择终点插入位置
                                if haveAny1(i, ways[j]):
                                    if k != len(ways[j]):
                                        k += 1
                                        continue
                                if k == len(ways[j]):
                                    pass
                                else:
                                    if ways[j][k] < 2 * orderNumberF:
                                        if k % 2 == 0:
                                            if orderF[int((ways[j][k]) / 2), 5] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderF[int((ways[j][k]) / 2), 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                    elif ways[j][k] < 2 * orderNumberF + orderNumberE1:
                                        if orderE1[ways[j][k] - 2 * orderNumberF, 7] < orderE1[
                                            i - 2 * orderNumberF, 7]:
                                            continue
                                        else:
                                            pass
                                    else:
                                        if orderE2[ways[j][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                            i - 2 * orderNumberF, 7]:
                                            continue
                                        else:
                                            pass

                                tempSolution = clone(ways[j])
                                # print("ways[j]=", ways[j])
                                # print("tempSolution=", tempSolution)
                                tempSolution.insert(k, i)  # 在此位置插入终点
                                # print("tempSolution=", tempSolution)
                                # print("bNum=", bNum)
                                needPoolAll = list(
                                    itertools.combinations(pool1, numberTug))  # 从pool1中取出needNumber个数
                                # print("needPoolAll=", needPoolAll)
                                for p in needPoolAll:
                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                    tempBracket = []
                                    tempBracket.append(needPools)
                                    needPools = tempBracket
                                    for needPool in needPools:
                                        # print("needPool=", needPool)
                                        for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                            if haveAny2(i, tempSolution):
                                                if pos != k:
                                                    pos += 1
                                                    continue
                                            if pos == k:
                                                pass
                                            else:
                                                if tempSolution[pos] < 2 * orderNumberF:
                                                    if tempSolution[pos] % 2 == 0:
                                                        if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            # print(pos)
                                                            pass
                                                    else:
                                                        if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                    if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < orderE2[
                                                        needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                        continue
                                                    else:
                                                        pass
                                                else:
                                                    if orderE2[
                                                        tempSolution[pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                            orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                        continue
                                                    else:
                                                        pass

                                            tempNeedPool = clone(needPool)
                                            tempTempSolution = clone(tempSolution)
                                            tempK = k
                                            tempTempSolution.insert(pos, tempNeedPool[
                                                0])  # 插入pool中的第一个元素
                                            # print("tempTempSolution=",tempTempSolution)
                                            tempNeedPool.remove(
                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                            tempK += 1
                                            if tempNeedPool != []:
                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                          pos, j,
                                                          needPool)
                                            else:
                                                temp = []
                                                for nub in range(courierNumber):
                                                    temp.append([])
                                                temp[j] = tempTempSolution
                                                tempLoss = lossFunction(temp, startTime,
                                                                        printf=False)

                                                if (tempLoss < bestLossE2):
                                                    # print("bestlossE2=",bestLossE2)
                                                    # print("tempLoss=", tempLoss)

                                                    bestLossE2 = tempLoss
                                                    bestJ = j
                                                    bestWay = clone(tempTempSolution)
                                                    bestPool = clone(needPool)

                            tempTempBestj.append(bestJ)
                            tempTempBestway.append(bestWay)
                            tempTempBestpool.append(bestPool)

                            tempPool = clone(pool1)
                            for point in bestPool:
                                tempPoint = point
                                tempPool.remove(tempPoint)

                            continue

                        if times == 1:  # 考虑第二次访问的情况

                            comNum[i - orderNumberF * 2][j][times] = nN

                            bestJ = 0  # 临时存储一下局部最优解
                            bestWay = []  # 临时存储一下局部最优解
                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                            bestPool = []  # 临时存储一下局部最优解

                            for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                # print("k=", k)
                                couldPos = tempTempBestway[0].index(i)
                                if k <= couldPos:
                                    continue
                                tempSolution = clone(tempTempBestway[0])
                                # print("ways[t]=", ways[t])
                                # print("tempSolution=", tempSolution)
                                tempSolution.insert(k, i)  # 在此位置插入终点
                                # print("tempSolution=", tempSolution)
                                # print("numberTug=", numberTug)
                                needPoolAll = list(itertools.combinations(tempPool, nN))  # 从pool1中取出needNumber个数
                                # print("needPoolAll=", needPoolAll)
                                for p in needPoolAll:
                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                    tempBracket = []
                                    tempBracket.append(needPools)
                                    needPools = tempBracket

                                    for needPool in needPools:
                                        # print("needPool=", needPool)
                                        for pos in range(couldPos + 1, k + 1):  # pos是pool中第一个元素要插入的位置
                                            tempNeedPool = clone(needPool)
                                            tempTempSolution = clone(tempSolution)
                                            tempK = k
                                            tempTempSolution.insert(pos, tempNeedPool[0])  # 插入pool中的第一个元素
                                            # print("tempTempSolution=",tempTempSolution)
                                            tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                            tempK += 1
                                            if tempNeedPool != []:
                                                cirInsert(tempK, tempTempSolution, tempNeedPool, pos, j,
                                                          needPool)
                                            else:
                                                temp = []
                                                for nub in range(courierNumber):
                                                    temp.append([])
                                                temp[j] = tempTempSolution
                                                tempLoss = lossFunction(temp, startTime, printf=False)
                                                if (tempLoss < bestLossE2):
                                                    # print("bestlossE2=",bestLossE2)
                                                    # print("tempLoss=", tempLoss)

                                                    bestLossE2 = tempLoss
                                                    bestJ = j
                                                    bestWay = clone(tempTempSolution)
                                                    bestPool = clone(needPool)

                            tempsolution = clone(ways)
                            tempsolution[bestJ] = bestWay

                            comNum[i - orderNumberF * 2][j][0] = numberTug
                            comNum[i - orderNumberF * 2][j][1] = nN

                            tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                            tempTempBestj.append(bestJ)
                            tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                            tempTempBestpool.append(bestPool)

                            if ti == 0:
                                bestTempBestloss = tempTempBestloss
                                bestTempBestj = tempTempBestj
                                bestTempBestway = tempTempBestway
                                bestTempBestpool = tempTempBestpool
                                ti += 1
                            else:
                                if bestTempBestloss > tempTempBestloss:
                                    bestTempBestloss = tempTempBestloss
                                    bestTempBestj = tempTempBestj
                                    bestTempBestway = tempTempBestway
                                    bestTempBestpool = tempTempBestpool
                                ti += 1

            if bestTempBestloss < BestLoss:
                BestLoss = bestTempBestloss
                BestJ = bestTempBestj
                BestWay = bestTempBestway
                BestPool = bestTempBestpool

            # 两拖船不重用
            bTempBestloss = float("inf")  # 两拖船不重用的最优解
            bTempBestj = []  # 两拖船不重用的最优解
            bTempBestway = []  # 两拖船不重用的最优解
            bTempBestpool = []  # 两拖船不重用的最优解
            tugNum = 2
            ti = 0
            tugAll = list(itertools.combinations(tugs, tugNum))
            # print("tugAll=",tugAll)
            for n in tugAll:
                tug = list(itertools.permutations(n))
                # print("tug=",tug)
                for tu in tug:
                    # print("tu=",tu)
                    # print("needNumber=",needNumber)
                    for bargeNum in range(needNumber):
                        # print("bargeNum=",bargeNum)
                        numberTug = needNumber  # numberTug是一个全局变量
                        bNum = bargeNum + 1  # bNum是第一艘拖船拉空驳船的数量
                        # print("bNum=",bNum)
                        numberTug -= bNum  # numberTug是现在还有多少空驳船需要拉
                        # print("numberTug=",numberTug)

                        if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (bNum == 1) or (bNum == 2):
                            # print("break")
                            continue

                        for t in tu:
                            first = tu[0]
                            last = tu[-1]
                            if t == first:
                                comNum[i - orderNumberF * 2][t][0] = bNum

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                tempPool = []  # 用于用于衔接两辆拖船

                                tempBestj = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestway = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestloss = 0  # 用于临时记录拖船的单拖船最优方案
                                tempBestpool = []  # 用于临时记录拖船的单拖船最优方案

                                # 不考虑重用
                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    # print("k=", k)
                                    tempSolution = clone(ways[t])
                                    # print("ways[j]=", ways[j])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("bNum=", bNum)
                                    needPoolAll = list(
                                        itertools.combinations(pool1, bNum))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket
                                        # if bNum==8:
                                        #     print(needPools)
                                        # if needPools==[[31,32,33,34,35,36,37,38]]:
                                        #     print("right here!")
                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)
                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = bNum

                                tempBestj.append(bestJ)  # t拖船的单拖船最优方案
                                tempBestway.append(bestWay)  # t拖船的单拖船最优方案
                                tempBestpool.append(bestPool)  # t拖船的单拖船最优方案

                                tempPool = clone(pool1)
                                for tim in range(len(tempBestj)):
                                    for point in tempBestpool[tim]:
                                        tempPoint = point
                                        tempPool.remove(tempPoint)

                                continue
                            elif t == last:
                                comNum[i - orderNumberF * 2][t][0] = numberTug
                                # print("comNum=", comNum)

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    # print("k=", k)
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[
                                                ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                    orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    tempSolution = clone(ways[t])
                                    # print("ways[t]=", ways[t])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("numberTug=", numberTug)
                                    needPoolAll = list(
                                        itertools.combinations(tempPool,
                                                               numberTug))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket

                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)

                                for timm in range(len(tempBestj)):
                                    tempsolution[tempBestj[timm]] = tempBestway[timm]

                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = numberTug

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                tempBestj.append(bestJ)
                                tempBestway.append(bestWay)
                                tempBestpool.append(bestPool)

                                if ti == 0:
                                    bTempBestloss = tempBestloss  # 两拖船不重用的最优解
                                    bTempBestj = tempBestj  # 两拖船不重用的最优解
                                    bTempBestway = tempBestway  # 两拖船不重用的最优解
                                    bTempBestpool = tempBestpool  # 两拖船不重用的最优解
                                    ti += 1
                                elif ti >= 1:
                                    if tempBestloss < bTempBestloss:
                                        bTempBestloss = tempBestloss  # 两拖船不重用的最优解
                                        bTempBestj = tempBestj  # 两拖船不重用的最优解
                                        bTempBestway = tempBestway  # 两拖船不重用的最优解
                                        bTempBestpool = tempBestpool  # 两拖船不重用的最优解
                                    ti += 1
            if bTempBestloss < BestLoss:
                BestLoss = bTempBestloss
                BestJ = bTempBestj
                BestWay = bTempBestway
                BestPool = bTempBestpool

        # 双拖船重用
        elif needNumber <= 3 * maxcap:
            tugNum = 2

            numberTug = 0  # 用于记录第二个拖船需要拉多少个驳船
            nTug1 = 0
            nTug2 = 0

            tugAll = list(itertools.combinations(tugs, tugNum))
            # print("tugAll=",tugAll)
            for n in tugAll:
                tug = list(itertools.permutations(n))
                # print("tug=",tug)
                for tu in tug:
                    # print("tu=",tu)
                    # print("needNumber=",needNumber)
                    for bargeNum in range(needNumber):
                        # print("bargeNum=",bargeNum)
                        numberTug = needNumber  # numberTug是一个全局变量
                        bNum = bargeNum + 1  # bNum是第一艘拖船拉空驳船的数量
                        # print("bNum=",bNum)
                        numberTug -= bNum  # numberTug是现在还有多少空驳船需要拉
                        # print("numberTug=",numberTug)

                        if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (bNum == 1) or (bNum == 2):
                            # print("break")
                            continue

                        for t in tu:
                            first = tu[0]
                            last = tu[-1]
                            if t == first:

                                bTempBestloss = 0  # 第一辆拖船的部分最优解
                                bTempBestj = []  # 第一辆拖船的部分最优解
                                bTempBestway = []  # 第一辆拖船的部分最优解
                                bTempBestpool = []  # 第一辆拖船的部分最优解

                                comNum[i - orderNumberF * 2][t][0] = bNum
                                # print("comNum=", comNum)
                                # print("t=",t)

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                tempPool = []  # 用于用于衔接两辆拖船

                                tempBestj = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestway = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestloss = 0  # 用于临时记录拖船的单拖船最优方案
                                tempBestpool = []  # 用于临时记录拖船的单拖船最优方案

                                # 不考虑重用
                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    # print("k=", k)
                                    tempSolution = clone(ways[t])
                                    # print("ways[j]=", ways[j])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("bNum=", bNum)
                                    needPoolAll = list(
                                        itertools.combinations(pool1, bNum))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket
                                        # if bNum==8:
                                        #     print(needPools)
                                        # if needPools==[[31,32,33,34,35,36,37,38]]:
                                        #     print("right here!")
                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)
                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = bNum

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)  # t拖船的单拖船最优方案
                                tempBestj.append(bestJ)  # t拖船的单拖船最优方案
                                tempBestway.append(bestWay)  # t拖船的单拖船最优方案
                                tempBestpool.append(bestPool)  # t拖船的单拖船最优方案

                                bTempBestloss = tempBestloss  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                bTempBestj = tempBestj  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                bTempBestway = tempBestway  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                bTempBestpool = tempBestpool  # 把t拖船的第一次得到的单拖船最优方案存在这里面

                                bestTempTempBestloss = float('inf')  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                bestTempTempBestj = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                bestTempTempBestway = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                bestTempTempBestpool = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较

                                ti = 0  # 用于记录是第几次得到了tempTempBestloss

                                # 考虑重用
                                for needM in range(bNum):
                                    nM = needM + 1  # 第一辆拖船第一次访问拉的驳船数
                                    nTug1 = bNum
                                    nTug1 -= nM  # 第一辆拖船第二次访问拉的驳船数
                                    if (nTug1 == 0) or (nTug1 == 1) or (nM == 1):
                                        continue
                                    for timess in range(maxTravelTime):

                                        if timess == 0:  # 考虑第一次访问的情况

                                            tempTempBestloss = 0  # 临时记录第一辆拖船的重用最优解
                                            tempTempBestj = []  # 临时记录第一辆拖船的重用最优解
                                            tempTempBestway = []  # 临时记录第一辆拖船的重用最优解
                                            tempTempBestpool = []  # 临时记录第一辆拖船的重用最优解

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            comNum[i - orderNumberF * 2][t][timess] = nTug1

                                            tempPooll = []  # 用于衔接两次访问

                                            for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                                # print("k=", k)
                                                if haveAny1(i, ways[t]):
                                                    if k != len(ways[t]):
                                                        k += 1
                                                        continue
                                                if k == len(ways[t]):
                                                    pass
                                                else:
                                                    if ways[t][k] < 2 * orderNumberF:
                                                        if k % 2 == 0:
                                                            if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                            i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[
                                                            ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass

                                                tempSolution = clone(ways[t])
                                                # print("ways[j]=", ways[t])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                needPoolAll = list(
                                                    itertools.combinations(pool1, nTug1))  # 从pool1中取出needNumber个数

                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket
                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            if haveAny2(i, tempSolution):
                                                                if pos != k:
                                                                    pos += 1
                                                                    continue
                                                            if pos == k:
                                                                pass
                                                            else:
                                                                if tempSolution[pos] < 2 * orderNumberF:
                                                                    if tempSolution[pos] % 2 == 0:
                                                                        if orderF[int((tempSolution[pos]) / 2), 5] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            # print(pos)
                                                                            pass
                                                                    else:
                                                                        if orderF[int((tempSolution[pos]) / 2), 7] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass
                                                                elif tempSolution[
                                                                    pos] < 2 * orderNumberF + orderNumberE1:
                                                                    if orderE1[
                                                                        tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass
                                                                else:
                                                                    if orderE2[tempSolution[
                                                                                   pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass

                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t, needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime,
                                                                                        printf=False)
                                                                # print("tempLoss=", tempLoss)

                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=", bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)
                                            tempTempBestpool.append(bestPool)

                                            tempPooll = clone(pool1)
                                            for point in bestPool:
                                                tempPoint = point
                                                tempPooll.remove(tempPoint)

                                            continue

                                        if timess == 1:  # 考虑第二次访问的情况

                                            comNum[i - orderNumberF * 2][t][timess] = nM

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                                # print("k=", k)
                                                couldPos = tempTempBestway[0].index(i)
                                                if k <= couldPos:
                                                    continue
                                                tempSolution = clone(tempTempBestway[0])
                                                # print("ways[t]=", ways[t])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                # print("numberTug=", numberTug)
                                                needPoolAll = list(
                                                    itertools.combinations(tempPooll,
                                                                           nM))  # 从pool1中取出needNumber个数
                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket

                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(couldPos + 1, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t,
                                                                          needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime,
                                                                                        printf=False)
                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=",bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempsolution = clone(ways)
                                            tempsolution[bestJ] = bestWay

                                            comNum[i - orderNumberF * 2][t][0] = nTug1
                                            comNum[i - orderNumberF * 2][t][1] = nM

                                            tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                                            tempTempBestpool.append(bestPool)
                                            if ti == 0:
                                                bestTempTempBestloss = tempTempBestloss
                                                bestTempTempBestj = tempTempBestj
                                                bestTempTempBestway = tempTempBestway
                                                bestTempTempBestpool = tempTempBestpool
                                                ti += 1
                                            else:
                                                if bestTempTempBestloss > tempTempBestloss:
                                                    bestTempTempBestloss = tempTempBestloss
                                                    bestTempTempBestj = tempTempBestj
                                                    bestTempTempBestway = tempTempBestway
                                                    bestTempTempBestpool = tempTempBestpool
                                                ti += 1

                                if bestTempTempBestloss < bTempBestloss:
                                    bTempBestloss = bestTempTempBestloss
                                    bTempBestj = bestTempTempBestj  # [a,b]
                                    bTempBestway = bestTempTempBestway  # [[a,b],[c,d]]
                                    bTempBestpool = bestTempTempBestpool  # [[a,b],[c,d]]

                                tempPool = clone(pool1)
                                for tim in range(len(bTempBestj)):
                                    for point in bTempBestpool[tim]:
                                        tempPoint = point
                                        tempPool.remove(tempPoint)

                                continue

                                # 至此第一个拖船的最优方案已经存在了bTempBestloss、bTempBestj、bTempBestway、bTempBestpool里面
                                # 以及供第二辆拖船选择的驳船都已经存放在tempPool里面了

                            elif t == last:  # maxTugNum=2时才会进入这里
                                # print("t=",t)#对于最后一个元素t来说，他只有一种选择

                                # 不考虑重用
                                comNum[i - orderNumberF * 2][t][0] = numberTug
                                # print("comNum=", comNum)

                                tempBestloss = 0  # 用于临时记录第二辆拖船不重用的最优解
                                tempBestj = []  # 用于临时记录第二辆拖船不重用的最优解
                                tempBestway = []  # 用于临时记录第二辆拖船不重用的最优解
                                tempBestpool = []  # 用于临时记录第二辆拖船不重用的最优解

                                tTempBestloss = 0  # 第二辆拖船的最优解
                                tTempBestj = []  # 第二辆拖船的最优解
                                tTempBestway = []  # 第二辆拖船的最优解
                                tTempBestpool = []  # 第二辆拖船的最优解

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    # print("k=", k)
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[
                                                ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                    orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    tempSolution = clone(ways[t])
                                    # print("ways[t]=", ways[t])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("numberTug=", numberTug)
                                    needPoolAll = list(
                                        itertools.combinations(tempPool,
                                                               numberTug))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket

                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)

                                for timm in range(len(bTempBestj)):  # bTempBestj、bTempBestway这些都是第一辆拖船的部分最优解
                                    tempsolution[bTempBestj[timm]] = bTempBestway[timm]

                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = numberTug

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                tempBestj.append(bestJ)
                                tempBestway.append(bestWay)
                                tempBestpool.append(bestPool)

                                tTempBestloss = tempBestloss  # 第二辆拖船的最优解
                                tTempBestj = tempBestj  # 第二辆拖船的最优解
                                tTempBestway = tempBestway  # 第二辆拖船的最优解
                                tTempBestpool = tempBestpool  # 第二辆拖船的最优解

                                bestTempTempBestloss = float("inf")  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                bestTempTempBestj = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                bestTempTempBestway = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                bestTempTempBestpool = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较

                                ti = 0

                                # 考虑重用
                                for needB in range(numberTug):
                                    nB = needB + 1  # 第一辆拖船第一次访问拉的驳船数
                                    nTug2 = numberTug
                                    nTug2 -= nB  # 第一辆拖船第二次访问拉的驳船数
                                    if (nTug2 == 0) or (nTug2 == 1) or (nB == 1):
                                        continue

                                    for timess in range(maxTravelTime):
                                        if timess == 0:  # 考虑第一次访问的情况

                                            tempTempBestloss = 0  # 用于临时记录第二辆拖船重用的最优解
                                            tempTempBestj = []  # 用于临时记录第二辆拖船重用的最优解
                                            tempTempBestway = []  # 用于临时记录第二辆拖船重用的最优解
                                            tempTempBestpool = []  # 用于临时记录第二辆拖船重用的最优解

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            comNum[i - orderNumberF * 2][t][timess] = nTug2

                                            ttempPool = []  # 用于衔接两次分配任务

                                            for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                                # print("k=", k)
                                                if haveAny1(i, ways[t]):
                                                    if k != len(ways[t]):
                                                        k += 1
                                                        continue
                                                if k == len(ways[t]):
                                                    pass
                                                else:
                                                    if ways[t][k] < 2 * orderNumberF:
                                                        if k % 2 == 0:
                                                            if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                            i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[
                                                            ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass

                                                tempSolution = clone(ways[t])
                                                # print("ways[j]=", ways[j])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                # print("bNum=", bNum)
                                                needPoolAll = list(
                                                    itertools.combinations(tempPool,
                                                                           nTug2))  # 从pool1中取出needNumber个数
                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket

                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            if haveAny2(i, tempSolution):
                                                                if pos != k:
                                                                    pos += 1
                                                                    continue
                                                            if pos == k:
                                                                pass
                                                            else:
                                                                if tempSolution[pos] < 2 * orderNumberF:
                                                                    if tempSolution[pos] % 2 == 0:
                                                                        if orderF[int((tempSolution[pos]) / 2), 5] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            # print(pos)
                                                                            pass
                                                                    else:
                                                                        if orderF[int((tempSolution[pos]) / 2), 7] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass
                                                                elif tempSolution[
                                                                    pos] < 2 * orderNumberF + orderNumberE1:
                                                                    if orderE1[
                                                                        tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass
                                                                else:
                                                                    if orderE2[tempSolution[
                                                                                   pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass

                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t,
                                                                          needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime, printf=False)

                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=",bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)
                                            tempTempBestpool.append(bestPool)

                                            comNum[i - orderNumberF * 2][t][timess] = nTug2

                                            ttempPool = clone(tempPool)
                                            for point in bestPool:
                                                tempPoint = point
                                                ttempPool.remove(tempPoint)

                                            continue

                                        if timess == 1:  # 考虑第二次访问的情况

                                            comNum[i - orderNumberF * 2][t][timess] = nB

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                                couldPos = tempTempBestway[0].index(i)
                                                if k <= couldPos:
                                                    continue
                                                # print("k=", k)
                                                tempSolution = clone(tempTempBestway[0])
                                                # print("ways[t]=", ways[t])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                # print("numberTug=", numberTug)
                                                needPoolAll = list(
                                                    itertools.combinations(ttempPool,
                                                                           nB))  # 从pool1中取出needNumber个数
                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket

                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(couldPos + 1, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t,
                                                                          needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime,
                                                                                        printf=False)
                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=",bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempsolution = clone(ways)
                                            tempsolution[bestJ] = bestWay

                                            comNum[i - orderNumberF * 2][t][0] = nTug2
                                            comNum[i - orderNumberF * 2][t][1] = nB

                                            tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                                            tempTempBestpool.append(bestPool)

                                            if ti == 0:
                                                bestTempTempBestloss = tempTempBestloss
                                                bestTempTempBestj = tempTempBestj
                                                bestTempTempBestway = tempTempBestway
                                                bestTempTempBestpool = tempTempBestpool
                                                ti += 1
                                            else:
                                                if bestTempTempBestloss > tempTempBestloss:
                                                    bestTempTempBestloss = tempTempBestloss
                                                    bestTempTempBestj = tempTempBestj
                                                    bestTempTempBestway = tempTempBestway
                                                    bestTempTempBestpool = tempTempBestpool
                                                ti += 1

                                if bestTempTempBestloss < tTempBestloss:
                                    tTempBestloss = bestTempTempBestloss
                                    tTempBestj = bestTempTempBestj  # [a,b]
                                    tTempBestway = bestTempTempBestway  # [[a,b],[c,d]]
                                    tTempBestpool = bestTempTempBestpool  # [[a,b],[c,d]]

                                # 至此，第一辆拖船以及第二辆拖船的最优部分解均已经收集完毕

                                for leng in range(len(tTempBestj)):
                                    bTempBestj.append(tTempBestj[leng])
                                    bTempBestway.append(tTempBestway[leng])
                                    bTempBestpool.append(tTempBestpool[leng])

                                tempsolution = clone(ways)

                                for leng in range(len(bTempBestj)):
                                    tempsolution[bTempBestj[leng]] = bTempBestway[leng]

                                tNum = []

                                for ii in range(orderNumberE1):
                                    tNum.append([])
                                    for jj in range(courierNumber):
                                        tNum[ii].append([])
                                        for _ in range(2):
                                            tNum[ii][jj].append(0)

                                comNum[i - orderNumberF * 2] = tNum[i - orderNumberF * 2]

                                tempp = []
                                for to in bTempBestj:
                                    if to in tempp:
                                        # 查找 to 在 bbestTempBestj 中第二次出现的位置
                                        index = find_nth_occurrence(bTempBestj, to, 2)
                                        if index != -1:
                                            comNum[i - orderNumberF * 2][to][1] = len(bTempBestpool[index])
                                    else:
                                        index = bTempBestj.index(to)
                                        comNum[i - orderNumberF * 2][to][0] = len(bTempBestpool[index])
                                    tempp.append(to)

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)

                                if tii == 0:
                                    bbestTempBestloss = tempBestloss
                                    bbestTempBestj = bTempBestj
                                    bbestTempBestway = bTempBestway
                                    bbestTempBestpool = bTempBestpool
                                    tii += 1
                                elif tii != 0:
                                    tii += 1
                                    if bbestTempBestloss >= tempBestloss:
                                        bbestTempBestloss = tempBestloss
                                        bbestTempBestj = bTempBestj
                                        bbestTempBestway = bTempBestway
                                        bbestTempBestpool = bTempBestpool
                if bbestTempBestloss < BestLoss:
                    BestLoss = bbestTempBestloss
                    BestJ = bbestTempBestj
                    BestWay = bbestTempBestway
                    BestPool = bbestTempBestpool

        pool1 = tempPool1
        for chosenone in range(len(BestJ)):
            ways[BestJ[chosenone]] = BestWay[chosenone]
            for point in BestPool[chosenone]:
                tempPoint = point
                pool1.remove(tempPoint)
        tempPool1 = pool1
        tNum = []

        for ii in range(orderNumberE1):
            tNum.append([])
            for jj in range(courierNumber):
                tNum[ii].append([])
                for _ in range(2):
                    tNum[ii][jj].append(0)

        comNum[i - orderNumberF * 2] = tNum[i - orderNumberF * 2]

        tempp = []
        for to in BestJ:
            if to in tempp:
                # 查找 to 在 bbestTempBestj 中第二次出现的位置
                index = find_nth_occurrence(BestJ, to, 2)
                if index != -1:
                    comNum[i - orderNumberF * 2][to][1] = len(BestPool[index])
            else:
                index = BestJ.index(to)
                comNum[i - orderNumberF * 2][to][0] = len(BestPool[index])
            tempp.append(to)
    pool1=[]
    pool2=[]
    return ways,pool1,pool2

# 自适应规模-深度贪婪修复E类型订单的某路径
def repairE2(ways, pool1, pool2):  # 贪婪重建算子
    global comNum  # 用于记录每个拖船在完成相应的E类型时第一次（如果有第二次的话）拉了几个空驳船

    global tugs  # 一会儿用它结合lossFunction操控拖船

    global bestJ  # 调用递归函数时要用
    global bestWay  # 调用递归函数时要用
    global bestLossE2  # 调用递归函数时要用
    global bestPool  # 调用递归函数时要用

    global numberTug  # 用于记录tugNum等于1时第二次访问拖船需要拉多少个驳船，以及tugNum等于二时第二艘拖船需要拉多少个驳船
    global nTug1  # 用于记录tugNum等于2时第一个拖船第二次访问拖船需要拉几个驳船
    global nTug2  # 用于记录tugNum等于2时第二个拖船第二次访问拖船需要拉几个驳船

    global maxTugNum  # 最多允许几个拖船共同完成一个E类型订单任务（默认为2
    global maxTravelTime  # 最多允许一个拖船访问同一个任务点几次（默认为2

    BestJJ=[]
    BestWayy=[]
    BestLosss=float("inf")
    BestPooll=[]
    BestI=0
    timer=0
    tempPool1 = pool1
    tempPool2=copy.deepcopy(pool1)

    for i in pool2:
        # print("i=", i)
        # print("pool1=", pool1)
        # print("pool2=", pool2)
        # print("ways=", ways)
        needNumber = int(orderE1[i - orderNumberF * 2][8])  # 获取E类型订单需要的空驳船数量
        pool1 = pool1[:needNumber]

        tii = 0  # 记录是第几次得到了临时最后解
        bbestTempBestloss = float("inf")  # 临时记录最后解
        bbestTempBestj = []  # 临时记录最后解
        bbestTempBestway = []  # 临时记录最后解
        bbestTempBestpool = []  # 临时记录最后解

        BestJ = []  # 记录单拖船不重用的最优解，记录最后的最优解
        BestWay = []  # 记录单拖船不重用的最优解，记录最后的最优解
        BestLoss = float('inf')  # 记录单拖船不重用的最优解，记录最后的最优解
        BestPool = []  # 记录单拖船不重用的最优解，记录最后的最优解

        bestj = []  # 临时存储一下单拖船不重用整体最优解
        bestway = []  # 临时存储一下单拖船不重用整体最优解
        bestloss = float('inf')  # 临时存储一下单拖船不重用整体最优解
        bestpool = []  # 临时存储一下单拖船不重用整体最优解

        # 单拖船不重用
        if needNumber <= maxcap:
            tugNum = 1
            for j in range(len(ways)):  # 选择拖船
                bestJ = 0  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                bestWay = []  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                bestLossE2 = float('inf')  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                bestPool = []  # 因为要调用递归函数，所以在这里初始化这四个全局变量

                comNum[i - orderNumberF * 2][j][0] = needNumber

                for k in range(len(ways[j]) + 1):  # 选择终点插入位置
                    if haveAny1(i, ways[j]):
                        if k != len(ways[j]):
                            k += 1
                            continue
                    if k == len(ways[j]):
                        pass
                    else:
                        if ways[j][k] < 2 * orderNumberF:
                            if k % 2 == 0:
                                if orderF[int((ways[j][k]) / 2), 5] < orderE1[i - 2 * orderNumberF, 7]:
                                    continue
                                else:
                                    pass
                            else:
                                if orderF[int((ways[j][k]) / 2), 7] < orderE1[i - 2 * orderNumberF, 7]:
                                    continue
                                else:
                                    pass
                        elif ways[j][k] < 2 * orderNumberF + orderNumberE1:
                            if orderE1[ways[j][k] - 2 * orderNumberF, 7] < orderE1[i - 2 * orderNumberF, 7]:
                                continue
                            else:
                                pass
                        else:
                            if orderE2[ways[j][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                i - 2 * orderNumberF, 7]:
                                continue
                            else:
                                pass
                    tempSolution = clone(ways[j])
                    # print("ways[j]=", ways[j])
                    # print("tempSolution=", tempSolution)
                    tempSolution.insert(k, i)  # 在此位置插入终点
                    # print("tempSolution=", tempSolution)
                    needPoolAll = list(itertools.combinations(pool1, needNumber))  # 从pool1中取出needNumber个数
                    for p in needPoolAll:
                        # print("p=", p)
                        needPools = list(sorted(p, key=lambda i: orderE2[
                            i - 2 * orderNumberF - orderNumberE1, 9]))
                        tempBracket = []
                        tempBracket.append(needPools)
                        needPools = tempBracket

                        for needPool in needPools:
                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                if haveAny2(i, tempSolution):
                                    if pos != k:
                                        pos += 1
                                        continue
                                if pos == k:
                                    pass
                                else:
                                    if tempSolution[pos] < 2 * orderNumberF:
                                        if tempSolution[pos] % 2 == 0:
                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                continue
                                            else:
                                                # print(pos)
                                                pass
                                        else:
                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                continue
                                            else:
                                                pass
                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < orderE2[
                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                            continue
                                        else:
                                            pass
                                    else:
                                        if orderE2[tempSolution[pos] - 2 * orderNumberF - orderNumberE1, 9] < orderE2[
                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                            continue
                                        else:
                                            pass
                                tempNeedPool = clone(needPool)
                                tempTempSolution = clone(tempSolution)
                                tempK = k
                                tempTempSolution.insert(pos, tempNeedPool[0])  # 插入pool中的第一个元素
                                # print("tempTempSolution=",tempTempSolution)
                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                tempK += 1
                                if tempNeedPool != []:
                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, j, needPool)
                                else:
                                    temp = []
                                    for nub in range(courierNumber):
                                        temp.append([])
                                    temp[j] = tempTempSolution
                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                    if (tempLoss < bestLossE2):
                                        # print("bestlossE2=",bestLossE2)
                                        # print("tempLoss=", tempLoss)

                                        bestLossE2 = tempLoss
                                        bestJ = j
                                        bestWay = clone(tempTempSolution)
                                        bestPool = clone(needPool)
                                        # print("bestLossE2=", bestLossE2)
                                        # print("bestPool=", bestPool)
                                        # print("bestWay=", bestWay)
                                        # print("bestJ=", bestJ)

                tempsolution = clone(ways)
                tempsolution[bestJ] = bestWay
                # print("bestWay=",bestWay)
                if (j == 0):
                    # print("j=",j)
                    bestloss = lossFunction(tempsolution, startTime, printf=False)
                    # print("bestloss=", bestloss)
                    bestj = bestJ
                    bestway = bestWay
                    bestpool = bestPool
                else:
                    # print("j=", j)
                    tempLoss = lossFunction(tempsolution, startTime, printf=False)
                    # print("tempLoss=", tempLoss)
                    if (tempLoss < bestloss):
                        bestloss = tempLoss
                        bestj = bestJ
                        bestway = bestWay
                        bestpool = bestPool

            BestLoss = bestloss  # 记录单拖船不重用的最优解，记录最后的最优解
            BestJ.append(bestj)  # 记录单拖船不重用的最优解，记录最后的最优解
            BestWay.append(bestway)  # 记录单拖船不重用的最优解，记录最后的最优解
            BestPool.append(bestpool)  # 记录单拖船不重用的最优解，记录最后的最优解

        # 单拖船重用+两拖船不重用
        elif needNumber <= 2 * maxcap:
            # 单拖船重用
            tugNum = 1
            bestTempBestloss = float("inf")  # 记录单拖船重用的最优解
            bestTempBestj = []  # 记录单拖船重用的最优解
            bestTempBestway = []  # 记录单拖船重用的最优解
            bestTempBestpool = []  # 记录单拖船重用的最优解

            ti = 0  # 记录是第几次得到tempBestj、tempBestway、tempBestloss、tempBestpool了

            for j in range(len(ways)):  # 选择拖船
                # print("j=",j)
                for needN in range(needNumber):
                    numberTug = needNumber  # numberTug是一个全局变量
                    nN = needN + 1  # bNum是第一次访问拉空驳船的数量
                    # print("bNum=",bNum)
                    numberTug -= nN  # numberTug是第二次访问拉空驳船的数量
                    if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (nN == 1) or (
                            nN == 2):  # numberTug=0意味着第二次访问拉的数量为0，相当于只考虑了访问一次
                        continue

                    for times in range(maxTravelTime):
                        if times == 0:  # 考虑第一次访问的情况

                            tempTempBestloss = 0  # 临时存储第一次访问与第二次访问的解
                            tempTempBestj = []  # 临时存储第一次访问与第二次访问的解
                            tempTempBestway = []  # 临时存储第一次访问与第二次访问的解
                            tempTempBestpool = []  # 临时存储第一次访问与第二次访问的解

                            comNum[i - orderNumberF * 2][j][times] = numberTug

                            bestJ = 0  # 临时存储一下局部最优解
                            bestWay = []  # 临时存储一下局部最优解
                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                            bestPool = []  # 临时存储一下局部最优解

                            tempPool = []  # 用于衔接两次分配任务

                            for k in range(len(ways[j]) + 1):  # 选择终点插入位置
                                if haveAny1(i, ways[j]):
                                    if k != len(ways[j]):
                                        k += 1
                                        continue
                                if k == len(ways[j]):
                                    pass
                                else:
                                    if ways[j][k] < 2 * orderNumberF:
                                        if k % 2 == 0:
                                            if orderF[int((ways[j][k]) / 2), 5] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderF[int((ways[j][k]) / 2), 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                    elif ways[j][k] < 2 * orderNumberF + orderNumberE1:
                                        if orderE1[ways[j][k] - 2 * orderNumberF, 7] < orderE1[
                                            i - 2 * orderNumberF, 7]:
                                            continue
                                        else:
                                            pass
                                    else:
                                        if orderE2[ways[j][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                            i - 2 * orderNumberF, 7]:
                                            continue
                                        else:
                                            pass

                                tempSolution = clone(ways[j])
                                # print("ways[j]=", ways[j])
                                # print("tempSolution=", tempSolution)
                                tempSolution.insert(k, i)  # 在此位置插入终点
                                # print("tempSolution=", tempSolution)
                                # print("bNum=", bNum)
                                needPoolAll = list(
                                    itertools.combinations(pool1, numberTug))  # 从pool1中取出needNumber个数
                                # print("needPoolAll=", needPoolAll)
                                for p in needPoolAll:
                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                    tempBracket = []
                                    tempBracket.append(needPools)
                                    needPools = tempBracket
                                    for needPool in needPools:
                                        # print("needPool=", needPool)
                                        for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                            if haveAny2(i, tempSolution):
                                                if pos != k:
                                                    pos += 1
                                                    continue
                                            if pos == k:
                                                pass
                                            else:
                                                if tempSolution[pos] < 2 * orderNumberF:
                                                    if tempSolution[pos] % 2 == 0:
                                                        if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            # print(pos)
                                                            pass
                                                    else:
                                                        if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                    if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < orderE2[
                                                        needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                        continue
                                                    else:
                                                        pass
                                                else:
                                                    if orderE2[
                                                        tempSolution[pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                            orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                        continue
                                                    else:
                                                        pass

                                            tempNeedPool = clone(needPool)
                                            tempTempSolution = clone(tempSolution)
                                            tempK = k
                                            tempTempSolution.insert(pos, tempNeedPool[
                                                0])  # 插入pool中的第一个元素
                                            # print("tempTempSolution=",tempTempSolution)
                                            tempNeedPool.remove(
                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                            tempK += 1
                                            if tempNeedPool != []:
                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                          pos, j,
                                                          needPool)
                                            else:
                                                temp = []
                                                for nub in range(courierNumber):
                                                    temp.append([])
                                                temp[j] = tempTempSolution
                                                tempLoss = lossFunction(temp, startTime,
                                                                        printf=False)

                                                if (tempLoss < bestLossE2):
                                                    # print("bestlossE2=",bestLossE2)
                                                    # print("tempLoss=", tempLoss)

                                                    bestLossE2 = tempLoss
                                                    bestJ = j
                                                    bestWay = clone(tempTempSolution)
                                                    bestPool = clone(needPool)

                            tempTempBestj.append(bestJ)
                            tempTempBestway.append(bestWay)
                            tempTempBestpool.append(bestPool)

                            tempPool = clone(pool1)
                            for point in bestPool:
                                tempPoint = point
                                tempPool.remove(tempPoint)

                            continue

                        if times == 1:  # 考虑第二次访问的情况

                            comNum[i - orderNumberF * 2][j][times] = nN

                            bestJ = 0  # 临时存储一下局部最优解
                            bestWay = []  # 临时存储一下局部最优解
                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                            bestPool = []  # 临时存储一下局部最优解

                            for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                # print("k=", k)
                                couldPos = tempTempBestway[0].index(i)
                                if k <= couldPos:
                                    continue
                                tempSolution = clone(tempTempBestway[0])
                                # print("ways[t]=", ways[t])
                                # print("tempSolution=", tempSolution)
                                tempSolution.insert(k, i)  # 在此位置插入终点
                                # print("tempSolution=", tempSolution)
                                # print("numberTug=", numberTug)
                                needPoolAll = list(itertools.combinations(tempPool, nN))  # 从pool1中取出needNumber个数
                                # print("needPoolAll=", needPoolAll)
                                for p in needPoolAll:
                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                    tempBracket = []
                                    tempBracket.append(needPools)
                                    needPools = tempBracket

                                    for needPool in needPools:
                                        # print("needPool=", needPool)
                                        for pos in range(couldPos + 1, k + 1):  # pos是pool中第一个元素要插入的位置
                                            tempNeedPool = clone(needPool)
                                            tempTempSolution = clone(tempSolution)
                                            tempK = k
                                            tempTempSolution.insert(pos, tempNeedPool[0])  # 插入pool中的第一个元素
                                            # print("tempTempSolution=",tempTempSolution)
                                            tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                            tempK += 1
                                            if tempNeedPool != []:
                                                cirInsert(tempK, tempTempSolution, tempNeedPool, pos, j,
                                                          needPool)
                                            else:
                                                temp = []
                                                for nub in range(courierNumber):
                                                    temp.append([])
                                                temp[j] = tempTempSolution
                                                tempLoss = lossFunction(temp, startTime, printf=False)
                                                if (tempLoss < bestLossE2):
                                                    # print("bestlossE2=",bestLossE2)
                                                    # print("tempLoss=", tempLoss)

                                                    bestLossE2 = tempLoss
                                                    bestJ = j
                                                    bestWay = clone(tempTempSolution)
                                                    bestPool = clone(needPool)

                            tempsolution = clone(ways)
                            tempsolution[bestJ] = bestWay

                            comNum[i - orderNumberF * 2][j][0] = numberTug
                            comNum[i - orderNumberF * 2][j][1] = nN

                            tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                            tempTempBestj.append(bestJ)
                            tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                            tempTempBestpool.append(bestPool)

                            if ti == 0:
                                bestTempBestloss = tempTempBestloss
                                bestTempBestj = tempTempBestj
                                bestTempBestway = tempTempBestway
                                bestTempBestpool = tempTempBestpool
                                ti += 1
                            else:
                                if bestTempBestloss > tempTempBestloss:
                                    bestTempBestloss = tempTempBestloss
                                    bestTempBestj = tempTempBestj
                                    bestTempBestway = tempTempBestway
                                    bestTempBestpool = tempTempBestpool
                                ti += 1

            if bestTempBestloss < BestLoss:
                BestLoss = bestTempBestloss
                BestJ = bestTempBestj
                BestWay = bestTempBestway
                BestPool = bestTempBestpool

            # 两拖船不重用
            bTempBestloss = float("inf")  # 两拖船不重用的最优解
            bTempBestj = []  # 两拖船不重用的最优解
            bTempBestway = []  # 两拖船不重用的最优解
            bTempBestpool = []  # 两拖船不重用的最优解
            tugNum = 2
            ti = 0
            tugAll = list(itertools.combinations(tugs, tugNum))
            # print("tugAll=",tugAll)
            for n in tugAll:
                tug = list(itertools.permutations(n))
                # print("tug=",tug)
                for tu in tug:
                    # print("tu=",tu)
                    # print("needNumber=",needNumber)
                    for bargeNum in range(needNumber):
                        # print("bargeNum=",bargeNum)
                        numberTug = needNumber  # numberTug是一个全局变量
                        bNum = bargeNum + 1  # bNum是第一艘拖船拉空驳船的数量
                        # print("bNum=",bNum)
                        numberTug -= bNum  # numberTug是现在还有多少空驳船需要拉
                        # print("numberTug=",numberTug)

                        if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (bNum == 1) or (bNum == 2):
                            # print("break")
                            continue

                        for t in tu:
                            first = tu[0]
                            last = tu[-1]
                            if t == first:
                                comNum[i - orderNumberF * 2][t][0] = bNum

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                tempPool = []  # 用于用于衔接两辆拖船

                                tempBestj = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestway = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestloss = 0  # 用于临时记录拖船的单拖船最优方案
                                tempBestpool = []  # 用于临时记录拖船的单拖船最优方案

                                # 不考虑重用
                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    # print("k=", k)
                                    tempSolution = clone(ways[t])
                                    # print("ways[j]=", ways[j])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("bNum=", bNum)
                                    needPoolAll = list(
                                        itertools.combinations(pool1, bNum))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket
                                        # if bNum==8:
                                        #     print(needPools)
                                        # if needPools==[[31,32,33,34,35,36,37,38]]:
                                        #     print("right here!")
                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)
                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = bNum

                                tempBestj.append(bestJ)  # t拖船的单拖船最优方案
                                tempBestway.append(bestWay)  # t拖船的单拖船最优方案
                                tempBestpool.append(bestPool)  # t拖船的单拖船最优方案

                                tempPool = clone(pool1)
                                for tim in range(len(tempBestj)):
                                    for point in tempBestpool[tim]:
                                        tempPoint = point
                                        tempPool.remove(tempPoint)

                                continue
                            elif t == last:
                                comNum[i - orderNumberF * 2][t][0] = numberTug
                                # print("comNum=", comNum)

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    # print("k=", k)
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[
                                                ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                    orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    tempSolution = clone(ways[t])
                                    # print("ways[t]=", ways[t])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("numberTug=", numberTug)
                                    needPoolAll = list(
                                        itertools.combinations(tempPool,
                                                               numberTug))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket

                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)

                                for timm in range(len(tempBestj)):
                                    tempsolution[tempBestj[timm]] = tempBestway[timm]

                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = numberTug

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                tempBestj.append(bestJ)
                                tempBestway.append(bestWay)
                                tempBestpool.append(bestPool)

                                if ti == 0:
                                    bTempBestloss = tempBestloss  # 两拖船不重用的最优解
                                    bTempBestj = tempBestj  # 两拖船不重用的最优解
                                    bTempBestway = tempBestway  # 两拖船不重用的最优解
                                    bTempBestpool = tempBestpool  # 两拖船不重用的最优解
                                    ti += 1
                                elif ti >= 1:
                                    if tempBestloss < bTempBestloss:
                                        bTempBestloss = tempBestloss  # 两拖船不重用的最优解
                                        bTempBestj = tempBestj  # 两拖船不重用的最优解
                                        bTempBestway = tempBestway  # 两拖船不重用的最优解
                                        bTempBestpool = tempBestpool  # 两拖船不重用的最优解
                                    ti += 1
            if bTempBestloss < BestLoss:
                BestLoss = bTempBestloss
                BestJ = bTempBestj
                BestWay = bTempBestway
                BestPool = bTempBestpool

        # 双拖船重用
        elif needNumber <= 3 * maxcap:
            tugNum = 2

            numberTug = 0  # 用于记录第二个拖船需要拉多少个驳船
            nTug1 = 0
            nTug2 = 0

            tugAll = list(itertools.combinations(tugs, tugNum))
            # print("tugAll=",tugAll)
            for n in tugAll:
                tug = list(itertools.permutations(n))
                # print("tug=",tug)
                for tu in tug:
                    # print("tu=",tu)
                    # print("needNumber=",needNumber)
                    for bargeNum in range(needNumber):
                        # print("bargeNum=",bargeNum)
                        numberTug = needNumber  # numberTug是一个全局变量
                        bNum = bargeNum + 1  # bNum是第一艘拖船拉空驳船的数量
                        # print("bNum=",bNum)
                        numberTug -= bNum  # numberTug是现在还有多少空驳船需要拉
                        # print("numberTug=",numberTug)

                        if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (bNum == 1) or (bNum == 2):
                            # print("break")
                            continue

                        for t in tu:
                            first = tu[0]
                            last = tu[-1]
                            if t == first:

                                bTempBestloss = 0  # 第一辆拖船的部分最优解
                                bTempBestj = []  # 第一辆拖船的部分最优解
                                bTempBestway = []  # 第一辆拖船的部分最优解
                                bTempBestpool = []  # 第一辆拖船的部分最优解

                                comNum[i - orderNumberF * 2][t][0] = bNum
                                # print("comNum=", comNum)
                                # print("t=",t)

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                tempPool = []  # 用于用于衔接两辆拖船

                                tempBestj = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestway = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestloss = 0  # 用于临时记录拖船的单拖船最优方案
                                tempBestpool = []  # 用于临时记录拖船的单拖船最优方案

                                # 不考虑重用
                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    # print("k=", k)
                                    tempSolution = clone(ways[t])
                                    # print("ways[j]=", ways[j])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("bNum=", bNum)
                                    needPoolAll = list(
                                        itertools.combinations(pool1, bNum))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket
                                        # if bNum==8:
                                        #     print(needPools)
                                        # if needPools==[[31,32,33,34,35,36,37,38]]:
                                        #     print("right here!")
                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)
                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = bNum

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)  # t拖船的单拖船最优方案
                                tempBestj.append(bestJ)  # t拖船的单拖船最优方案
                                tempBestway.append(bestWay)  # t拖船的单拖船最优方案
                                tempBestpool.append(bestPool)  # t拖船的单拖船最优方案

                                bTempBestloss = tempBestloss  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                bTempBestj = tempBestj  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                bTempBestway = tempBestway  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                bTempBestpool = tempBestpool  # 把t拖船的第一次得到的单拖船最优方案存在这里面

                                bestTempTempBestloss = float('inf')  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                bestTempTempBestj = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                bestTempTempBestway = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                bestTempTempBestpool = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较

                                ti = 0  # 用于记录是第几次得到了tempTempBestloss

                                # 考虑重用
                                for needM in range(bNum):
                                    nM = needM + 1  # 第一辆拖船第一次访问拉的驳船数
                                    nTug1 = bNum
                                    nTug1 -= nM  # 第一辆拖船第二次访问拉的驳船数
                                    if (nTug1 == 0) or (nTug1 == 1) or (nM == 1):
                                        continue
                                    for timess in range(maxTravelTime):

                                        if timess == 0:  # 考虑第一次访问的情况

                                            tempTempBestloss = 0  # 临时记录第一辆拖船的重用最优解
                                            tempTempBestj = []  # 临时记录第一辆拖船的重用最优解
                                            tempTempBestway = []  # 临时记录第一辆拖船的重用最优解
                                            tempTempBestpool = []  # 临时记录第一辆拖船的重用最优解

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            comNum[i - orderNumberF * 2][t][timess] = nTug1

                                            tempPooll = []  # 用于衔接两次访问

                                            for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                                # print("k=", k)
                                                if haveAny1(i, ways[t]):
                                                    if k != len(ways[t]):
                                                        k += 1
                                                        continue
                                                if k == len(ways[t]):
                                                    pass
                                                else:
                                                    if ways[t][k] < 2 * orderNumberF:
                                                        if k % 2 == 0:
                                                            if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                            i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[
                                                            ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass

                                                tempSolution = clone(ways[t])
                                                # print("ways[j]=", ways[t])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                needPoolAll = list(
                                                    itertools.combinations(pool1, nTug1))  # 从pool1中取出needNumber个数

                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket
                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            if haveAny2(i, tempSolution):
                                                                if pos != k:
                                                                    pos += 1
                                                                    continue
                                                            if pos == k:
                                                                pass
                                                            else:
                                                                if tempSolution[pos] < 2 * orderNumberF:
                                                                    if tempSolution[pos] % 2 == 0:
                                                                        if orderF[int((tempSolution[pos]) / 2), 5] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            # print(pos)
                                                                            pass
                                                                    else:
                                                                        if orderF[int((tempSolution[pos]) / 2), 7] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass
                                                                elif tempSolution[
                                                                    pos] < 2 * orderNumberF + orderNumberE1:
                                                                    if orderE1[
                                                                        tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass
                                                                else:
                                                                    if orderE2[tempSolution[
                                                                                   pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass

                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t, needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime,
                                                                                        printf=False)
                                                                # print("tempLoss=", tempLoss)

                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=", bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)
                                            tempTempBestpool.append(bestPool)

                                            tempPooll = clone(pool1)
                                            for point in bestPool:
                                                tempPoint = point
                                                tempPooll.remove(tempPoint)

                                            continue

                                        if timess == 1:  # 考虑第二次访问的情况

                                            comNum[i - orderNumberF * 2][t][timess] = nM

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                                # print("k=", k)
                                                couldPos = tempTempBestway[0].index(i)
                                                if k <= couldPos:
                                                    continue
                                                tempSolution = clone(tempTempBestway[0])
                                                # print("ways[t]=", ways[t])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                # print("numberTug=", numberTug)
                                                needPoolAll = list(
                                                    itertools.combinations(tempPooll,
                                                                           nM))  # 从pool1中取出needNumber个数
                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket

                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(couldPos + 1, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t,
                                                                          needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime,
                                                                                        printf=False)
                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=",bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempsolution = clone(ways)
                                            tempsolution[bestJ] = bestWay

                                            comNum[i - orderNumberF * 2][t][0] = nTug1
                                            comNum[i - orderNumberF * 2][t][1] = nM

                                            tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                                            tempTempBestpool.append(bestPool)
                                            if ti == 0:
                                                bestTempTempBestloss = tempTempBestloss
                                                bestTempTempBestj = tempTempBestj
                                                bestTempTempBestway = tempTempBestway
                                                bestTempTempBestpool = tempTempBestpool
                                                ti += 1
                                            else:
                                                if bestTempTempBestloss > tempTempBestloss:
                                                    bestTempTempBestloss = tempTempBestloss
                                                    bestTempTempBestj = tempTempBestj
                                                    bestTempTempBestway = tempTempBestway
                                                    bestTempTempBestpool = tempTempBestpool
                                                ti += 1

                                if bestTempTempBestloss < bTempBestloss:
                                    bTempBestloss = bestTempTempBestloss
                                    bTempBestj = bestTempTempBestj  # [a,b]
                                    bTempBestway = bestTempTempBestway  # [[a,b],[c,d]]
                                    bTempBestpool = bestTempTempBestpool  # [[a,b],[c,d]]

                                tempPool = clone(pool1)
                                for tim in range(len(bTempBestj)):
                                    for point in bTempBestpool[tim]:
                                        tempPoint = point
                                        tempPool.remove(tempPoint)

                                continue

                                # 至此第一个拖船的最优方案已经存在了bTempBestloss、bTempBestj、bTempBestway、bTempBestpool里面
                                # 以及供第二辆拖船选择的驳船都已经存放在tempPool里面了

                            elif t == last:  # maxTugNum=2时才会进入这里
                                # print("t=",t)#对于最后一个元素t来说，他只有一种选择

                                # 不考虑重用
                                comNum[i - orderNumberF * 2][t][0] = numberTug
                                # print("comNum=", comNum)

                                tempBestloss = 0  # 用于临时记录第二辆拖船不重用的最优解
                                tempBestj = []  # 用于临时记录第二辆拖船不重用的最优解
                                tempBestway = []  # 用于临时记录第二辆拖船不重用的最优解
                                tempBestpool = []  # 用于临时记录第二辆拖船不重用的最优解

                                tTempBestloss = 0  # 第二辆拖船的最优解
                                tTempBestj = []  # 第二辆拖船的最优解
                                tTempBestway = []  # 第二辆拖船的最优解
                                tTempBestpool = []  # 第二辆拖船的最优解

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    # print("k=", k)
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[
                                                ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                    orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    tempSolution = clone(ways[t])
                                    # print("ways[t]=", ways[t])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("numberTug=", numberTug)
                                    needPoolAll = list(
                                        itertools.combinations(tempPool,
                                                               numberTug))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket

                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)

                                for timm in range(len(bTempBestj)):  # bTempBestj、bTempBestway这些都是第一辆拖船的部分最优解
                                    tempsolution[bTempBestj[timm]] = bTempBestway[timm]

                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = numberTug

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                tempBestj.append(bestJ)
                                tempBestway.append(bestWay)
                                tempBestpool.append(bestPool)

                                tTempBestloss = tempBestloss  # 第二辆拖船的最优解
                                tTempBestj = tempBestj  # 第二辆拖船的最优解
                                tTempBestway = tempBestway  # 第二辆拖船的最优解
                                tTempBestpool = tempBestpool  # 第二辆拖船的最优解

                                bestTempTempBestloss = float("inf")  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                bestTempTempBestj = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                bestTempTempBestway = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                bestTempTempBestpool = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较

                                ti = 0

                                # 考虑重用
                                for needB in range(numberTug):
                                    nB = needB + 1  # 第一辆拖船第一次访问拉的驳船数
                                    nTug2 = numberTug
                                    nTug2 -= nB  # 第一辆拖船第二次访问拉的驳船数
                                    if (nTug2 == 0) or (nTug2 == 1) or (nB == 1):
                                        continue

                                    for timess in range(maxTravelTime):
                                        if timess == 0:  # 考虑第一次访问的情况

                                            tempTempBestloss = 0  # 用于临时记录第二辆拖船重用的最优解
                                            tempTempBestj = []  # 用于临时记录第二辆拖船重用的最优解
                                            tempTempBestway = []  # 用于临时记录第二辆拖船重用的最优解
                                            tempTempBestpool = []  # 用于临时记录第二辆拖船重用的最优解

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            comNum[i - orderNumberF * 2][t][timess] = nTug2

                                            ttempPool = []  # 用于衔接两次分配任务

                                            for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                                # print("k=", k)
                                                if haveAny1(i, ways[t]):
                                                    if k != len(ways[t]):
                                                        k += 1
                                                        continue
                                                if k == len(ways[t]):
                                                    pass
                                                else:
                                                    if ways[t][k] < 2 * orderNumberF:
                                                        if k % 2 == 0:
                                                            if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                            i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[
                                                            ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass

                                                tempSolution = clone(ways[t])
                                                # print("ways[j]=", ways[j])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                # print("bNum=", bNum)
                                                needPoolAll = list(
                                                    itertools.combinations(tempPool,
                                                                           nTug2))  # 从pool1中取出needNumber个数
                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket

                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            if haveAny2(i, tempSolution):
                                                                if pos != k:
                                                                    pos += 1
                                                                    continue
                                                            if pos == k:
                                                                pass
                                                            else:
                                                                if tempSolution[pos] < 2 * orderNumberF:
                                                                    if tempSolution[pos] % 2 == 0:
                                                                        if orderF[int((tempSolution[pos]) / 2), 5] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            # print(pos)
                                                                            pass
                                                                    else:
                                                                        if orderF[int((tempSolution[pos]) / 2), 7] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass
                                                                elif tempSolution[
                                                                    pos] < 2 * orderNumberF + orderNumberE1:
                                                                    if orderE1[
                                                                        tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass
                                                                else:
                                                                    if orderE2[tempSolution[
                                                                                   pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass

                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t,
                                                                          needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime, printf=False)

                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=",bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)
                                            tempTempBestpool.append(bestPool)

                                            comNum[i - orderNumberF * 2][t][timess] = nTug2

                                            ttempPool = clone(tempPool)
                                            for point in bestPool:
                                                tempPoint = point
                                                ttempPool.remove(tempPoint)

                                            continue

                                        if timess == 1:  # 考虑第二次访问的情况

                                            comNum[i - orderNumberF * 2][t][timess] = nB

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                                couldPos = tempTempBestway[0].index(i)
                                                if k <= couldPos:
                                                    continue
                                                # print("k=", k)
                                                tempSolution = clone(tempTempBestway[0])
                                                # print("ways[t]=", ways[t])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                # print("numberTug=", numberTug)
                                                needPoolAll = list(
                                                    itertools.combinations(ttempPool,
                                                                           nB))  # 从pool1中取出needNumber个数
                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket

                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(couldPos + 1, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t,
                                                                          needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime,
                                                                                        printf=False)
                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=",bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempsolution = clone(ways)
                                            tempsolution[bestJ] = bestWay

                                            comNum[i - orderNumberF * 2][t][0] = nTug2
                                            comNum[i - orderNumberF * 2][t][1] = nB

                                            tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                                            tempTempBestpool.append(bestPool)

                                            if ti == 0:
                                                bestTempTempBestloss = tempTempBestloss
                                                bestTempTempBestj = tempTempBestj
                                                bestTempTempBestway = tempTempBestway
                                                bestTempTempBestpool = tempTempBestpool
                                                ti += 1
                                            else:
                                                if bestTempTempBestloss > tempTempBestloss:
                                                    bestTempTempBestloss = tempTempBestloss
                                                    bestTempTempBestj = tempTempBestj
                                                    bestTempTempBestway = tempTempBestway
                                                    bestTempTempBestpool = tempTempBestpool
                                                ti += 1

                                if bestTempTempBestloss < tTempBestloss:
                                    tTempBestloss = bestTempTempBestloss
                                    tTempBestj = bestTempTempBestj  # [a,b]
                                    tTempBestway = bestTempTempBestway  # [[a,b],[c,d]]
                                    tTempBestpool = bestTempTempBestpool  # [[a,b],[c,d]]

                                # 至此，第一辆拖船以及第二辆拖船的最优部分解均已经收集完毕

                                for leng in range(len(tTempBestj)):
                                    bTempBestj.append(tTempBestj[leng])
                                    bTempBestway.append(tTempBestway[leng])
                                    bTempBestpool.append(tTempBestpool[leng])

                                tempsolution = clone(ways)

                                for leng in range(len(bTempBestj)):
                                    tempsolution[bTempBestj[leng]] = bTempBestway[leng]

                                tNum = []

                                for ii in range(orderNumberE1):
                                    tNum.append([])
                                    for jj in range(courierNumber):
                                        tNum[ii].append([])
                                        for _ in range(2):
                                            tNum[ii][jj].append(0)

                                comNum[i - orderNumberF * 2] = tNum[i - orderNumberF * 2]

                                tempp = []
                                for to in bTempBestj:
                                    if to in tempp:
                                        # 查找 to 在 bbestTempBestj 中第二次出现的位置
                                        index = find_nth_occurrence(bTempBestj, to, 2)
                                        if index != -1:
                                            comNum[i - orderNumberF * 2][to][1] = len(bTempBestpool[index])
                                    else:
                                        index = bTempBestj.index(to)
                                        comNum[i - orderNumberF * 2][to][0] = len(bTempBestpool[index])
                                    tempp.append(to)

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)

                                if tii == 0:
                                    bbestTempBestloss = tempBestloss
                                    bbestTempBestj = bTempBestj
                                    bbestTempBestway = bTempBestway
                                    bbestTempBestpool = bTempBestpool
                                    tii += 1
                                elif tii != 0:
                                    tii += 1
                                    if bbestTempBestloss >= tempBestloss:
                                        bbestTempBestloss = tempBestloss
                                        bbestTempBestj = bTempBestj
                                        bbestTempBestway = bTempBestway
                                        bbestTempBestpool = bTempBestpool
                if bbestTempBestloss < BestLoss:
                    BestLoss = bbestTempBestloss
                    BestJ = bbestTempBestj
                    BestWay = bbestTempBestway
                    BestPool = bbestTempBestpool

        pool1 = tempPool1
        for chosenone in range(len(BestJ)):
            for point in BestPool[chosenone]:
                tempPoint = point
                pool1.remove(tempPoint)
        tempPool1 = pool1

        if timer==0:
            BestJJ=BestJ
            BestWayy=BestWay
            BestLosss=BestLoss
            BestPooll=BestPool
            BestI=i
            timer+=1
        elif timer!=0:
            if BestLoss<BestLosss:
                BestJJ = BestJ
                BestWayy = BestWay
                BestLosss = BestLoss
                BestPooll = BestPool
                BestI = i

    pool1 = tempPool2
    print("tempPool2=", tempPool2)
    print("pool1=", pool1)
    print("BestJJ=", BestJJ)

    if BestJJ!=[]:
        for chosenone in range(len(BestJJ)):
            ways[BestJJ[chosenone]] = BestWayy[chosenone]
            for point in BestPooll[chosenone]:
                tempPoint = point
                pool1.remove(tempPoint)
        tNum = []
        for ii in range(orderNumberE1):
            tNum.append([])
            for jj in range(courierNumber):
                tNum[ii].append([])
                for _ in range(2):
                    tNum[ii][jj].append(0)

        comNum[BestI - orderNumberF * 2] = tNum[BestI - orderNumberF * 2]

        tempp = []
        for to in BestJJ:
            if to in tempp:
                # 查找 to 在 bbestTempBestj 中第二次出现的位置
                index = find_nth_occurrence(BestJJ, to, 2)
                if index != -1:
                    comNum[BestI - orderNumberF * 2][to][1] = len(BestPooll[index])
            else:
                index = BestJJ.index(to)
                comNum[BestI - orderNumberF * 2][to][0] = len(BestPooll[index])
            tempp.append(to)

        pool2.remove(BestI)

    if pool2!=[]:
        for i in pool2:
            # print("i=", i)
            # print("pool1=", pool1)
            # print("pool2=", pool2)
            # print("ways=", ways)
            needNumber = int(orderE1[i - orderNumberF * 2][8])  # 获取E类型订单需要的空驳船数量
            pool1 = pool1[:needNumber]

            tii = 0  # 记录是第几次得到了临时最后解
            bbestTempBestloss = float("inf")  # 临时记录最后解
            bbestTempBestj = []  # 临时记录最后解
            bbestTempBestway = []  # 临时记录最后解
            bbestTempBestpool = []  # 临时记录最后解

            BestJ = []  # 记录单拖船不重用的最优解，记录最后的最优解
            BestWay = []  # 记录单拖船不重用的最优解，记录最后的最优解
            BestLoss = float('inf')  # 记录单拖船不重用的最优解，记录最后的最优解
            BestPool = []  # 记录单拖船不重用的最优解，记录最后的最优解

            bestj = []  # 临时存储一下单拖船不重用整体最优解
            bestway = []  # 临时存储一下单拖船不重用整体最优解
            bestloss = float('inf')  # 临时存储一下单拖船不重用整体最优解
            bestpool = []  # 临时存储一下单拖船不重用整体最优解

            # 单拖船不重用
            if needNumber <= maxcap:
                tugNum = 1
                for j in range(len(ways)):  # 选择拖船
                    bestJ = 0  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                    bestWay = []  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                    bestLossE2 = float('inf')  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                    bestPool = []  # 因为要调用递归函数，所以在这里初始化这四个全局变量

                    comNum[i - orderNumberF * 2][j][0] = needNumber

                    for k in range(len(ways[j]) + 1):  # 选择终点插入位置
                        if haveAny1(i, ways[j]):
                            if k != len(ways[j]):
                                k += 1
                                continue
                        if k == len(ways[j]):
                            pass
                        else:
                            if ways[j][k] < 2 * orderNumberF:
                                if k % 2 == 0:
                                    if orderF[int((ways[j][k]) / 2), 5] < orderE1[i - 2 * orderNumberF, 7]:
                                        continue
                                    else:
                                        pass
                                else:
                                    if orderF[int((ways[j][k]) / 2), 7] < orderE1[i - 2 * orderNumberF, 7]:
                                        continue
                                    else:
                                        pass
                            elif ways[j][k] < 2 * orderNumberF + orderNumberE1:
                                if orderE1[ways[j][k] - 2 * orderNumberF, 7] < orderE1[i - 2 * orderNumberF, 7]:
                                    continue
                                else:
                                    pass
                            else:
                                if orderE2[ways[j][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                    i - 2 * orderNumberF, 7]:
                                    continue
                                else:
                                    pass
                        tempSolution = clone(ways[j])
                        # print("ways[j]=", ways[j])
                        # print("tempSolution=", tempSolution)
                        tempSolution.insert(k, i)  # 在此位置插入终点
                        # print("tempSolution=", tempSolution)
                        needPoolAll = list(itertools.combinations(pool1, needNumber))  # 从pool1中取出needNumber个数
                        for p in needPoolAll:
                            # print("p=", p)
                            needPools = list(sorted(p, key=lambda i: orderE2[
                                i - 2 * orderNumberF - orderNumberE1, 9]))
                            tempBracket = []
                            tempBracket.append(needPools)
                            needPools = tempBracket

                            for needPool in needPools:
                                for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                    if haveAny2(i, tempSolution):
                                        if pos != k:
                                            pos += 1
                                            continue
                                    if pos == k:
                                        pass
                                    else:
                                        if tempSolution[pos] < 2 * orderNumberF:
                                            if tempSolution[pos] % 2 == 0:
                                                if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                    continue
                                                else:
                                                    # print(pos)
                                                    pass
                                            else:
                                                if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                    continue
                                                else:
                                                    pass
                                        elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < orderE2[
                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[tempSolution[pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                    orderE2[
                                                        needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                continue
                                            else:
                                                pass
                                    tempNeedPool = clone(needPool)
                                    tempTempSolution = clone(tempSolution)
                                    tempK = k
                                    tempTempSolution.insert(pos, tempNeedPool[0])  # 插入pool中的第一个元素
                                    # print("tempTempSolution=",tempTempSolution)
                                    tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                    tempK += 1
                                    if tempNeedPool != []:
                                        cirInsert(tempK, tempTempSolution, tempNeedPool, pos, j, needPool)
                                    else:
                                        temp = []
                                        for nub in range(courierNumber):
                                            temp.append([])
                                        temp[j] = tempTempSolution
                                        tempLoss = lossFunction(temp, startTime, printf=False)
                                        if (tempLoss < bestLossE2):
                                            # print("bestlossE2=",bestLossE2)
                                            # print("tempLoss=", tempLoss)

                                            bestLossE2 = tempLoss
                                            bestJ = j
                                            bestWay = clone(tempTempSolution)
                                            bestPool = clone(needPool)
                                            # print("bestLossE2=", bestLossE2)
                                            # print("bestPool=", bestPool)
                                            # print("bestWay=", bestWay)
                                            # print("bestJ=", bestJ)

                    tempsolution = clone(ways)
                    tempsolution[bestJ] = bestWay
                    # print("bestWay=",bestWay)
                    if (j == 0):
                        # print("j=",j)
                        bestloss = lossFunction(tempsolution, startTime, printf=False)
                        # print("bestloss=", bestloss)
                        bestj = bestJ
                        bestway = bestWay
                        bestpool = bestPool
                    else:
                        # print("j=", j)
                        tempLoss = lossFunction(tempsolution, startTime, printf=False)
                        # print("tempLoss=", tempLoss)
                        if (tempLoss < bestloss):
                            bestloss = tempLoss
                            bestj = bestJ
                            bestway = bestWay
                            bestpool = bestPool

                BestLoss = bestloss  # 记录单拖船不重用的最优解，记录最后的最优解
                BestJ.append(bestj)  # 记录单拖船不重用的最优解，记录最后的最优解
                BestWay.append(bestway)  # 记录单拖船不重用的最优解，记录最后的最优解
                BestPool.append(bestpool)  # 记录单拖船不重用的最优解，记录最后的最优解

            # 单拖船重用+两拖船不重用
            elif needNumber <= 2 * maxcap:
                # 单拖船重用
                tugNum = 1
                bestTempBestloss = float("inf")  # 记录单拖船重用的最优解
                bestTempBestj = []  # 记录单拖船重用的最优解
                bestTempBestway = []  # 记录单拖船重用的最优解
                bestTempBestpool = []  # 记录单拖船重用的最优解

                ti = 0  # 记录是第几次得到tempBestj、tempBestway、tempBestloss、tempBestpool了

                for j in range(len(ways)):  # 选择拖船
                    # print("j=",j)
                    for needN in range(needNumber):
                        numberTug = needNumber  # numberTug是一个全局变量
                        nN = needN + 1  # bNum是第一次访问拉空驳船的数量
                        # print("bNum=",bNum)
                        numberTug -= nN  # numberTug是第二次访问拉空驳船的数量
                        if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (nN == 1) or (
                                nN == 2):  # numberTug=0意味着第二次访问拉的数量为0，相当于只考虑了访问一次
                            continue

                        for times in range(maxTravelTime):
                            if times == 0:  # 考虑第一次访问的情况

                                tempTempBestloss = 0  # 临时存储第一次访问与第二次访问的解
                                tempTempBestj = []  # 临时存储第一次访问与第二次访问的解
                                tempTempBestway = []  # 临时存储第一次访问与第二次访问的解
                                tempTempBestpool = []  # 临时存储第一次访问与第二次访问的解

                                comNum[i - orderNumberF * 2][j][times] = numberTug

                                bestJ = 0  # 临时存储一下局部最优解
                                bestWay = []  # 临时存储一下局部最优解
                                bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                bestPool = []  # 临时存储一下局部最优解

                                tempPool = []  # 用于衔接两次分配任务

                                for k in range(len(ways[j]) + 1):  # 选择终点插入位置
                                    if haveAny1(i, ways[j]):
                                        if k != len(ways[j]):
                                            k += 1
                                            continue
                                    if k == len(ways[j]):
                                        pass
                                    else:
                                        if ways[j][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[j][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[j][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[j][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[j][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[ways[j][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    tempSolution = clone(ways[j])
                                    # print("ways[j]=", ways[j])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("bNum=", bNum)
                                    needPoolAll = list(
                                        itertools.combinations(pool1, numberTug))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket
                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < orderE2[
                                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[
                                                            tempSolution[pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos, tempNeedPool[
                                                    0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(
                                                    tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                              pos, j,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[j] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime,
                                                                            printf=False)

                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = j
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempTempBestj.append(bestJ)
                                tempTempBestway.append(bestWay)
                                tempTempBestpool.append(bestPool)

                                tempPool = clone(pool1)
                                for point in bestPool:
                                    tempPoint = point
                                    tempPool.remove(tempPoint)

                                continue

                            if times == 1:  # 考虑第二次访问的情况

                                comNum[i - orderNumberF * 2][j][times] = nN

                                bestJ = 0  # 临时存储一下局部最优解
                                bestWay = []  # 临时存储一下局部最优解
                                bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                bestPool = []  # 临时存储一下局部最优解

                                for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                    # print("k=", k)
                                    couldPos = tempTempBestway[0].index(i)
                                    if k <= couldPos:
                                        continue
                                    tempSolution = clone(tempTempBestway[0])
                                    # print("ways[t]=", ways[t])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("numberTug=", numberTug)
                                    needPoolAll = list(itertools.combinations(tempPool, nN))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket

                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(couldPos + 1, k + 1):  # pos是pool中第一个元素要插入的位置
                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos, tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, j,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[j] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = j
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)
                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][j][0] = numberTug
                                comNum[i - orderNumberF * 2][j][1] = nN

                                tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                tempTempBestj.append(bestJ)
                                tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                                tempTempBestpool.append(bestPool)

                                if ti == 0:
                                    bestTempBestloss = tempTempBestloss
                                    bestTempBestj = tempTempBestj
                                    bestTempBestway = tempTempBestway
                                    bestTempBestpool = tempTempBestpool
                                    ti += 1
                                else:
                                    if bestTempBestloss > tempTempBestloss:
                                        bestTempBestloss = tempTempBestloss
                                        bestTempBestj = tempTempBestj
                                        bestTempBestway = tempTempBestway
                                        bestTempBestpool = tempTempBestpool
                                    ti += 1

                if bestTempBestloss < BestLoss:
                    BestLoss = bestTempBestloss
                    BestJ = bestTempBestj
                    BestWay = bestTempBestway
                    BestPool = bestTempBestpool

                # 两拖船不重用
                bTempBestloss = float("inf")  # 两拖船不重用的最优解
                bTempBestj = []  # 两拖船不重用的最优解
                bTempBestway = []  # 两拖船不重用的最优解
                bTempBestpool = []  # 两拖船不重用的最优解
                tugNum = 2
                ti = 0
                tugAll = list(itertools.combinations(tugs, tugNum))
                # print("tugAll=",tugAll)
                for n in tugAll:
                    tug = list(itertools.permutations(n))
                    # print("tug=",tug)
                    for tu in tug:
                        # print("tu=",tu)
                        # print("needNumber=",needNumber)
                        for bargeNum in range(needNumber):
                            # print("bargeNum=",bargeNum)
                            numberTug = needNumber  # numberTug是一个全局变量
                            bNum = bargeNum + 1  # bNum是第一艘拖船拉空驳船的数量
                            # print("bNum=",bNum)
                            numberTug -= bNum  # numberTug是现在还有多少空驳船需要拉
                            # print("numberTug=",numberTug)

                            if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (bNum == 1) or (bNum == 2):
                                # print("break")
                                continue

                            for t in tu:
                                first = tu[0]
                                last = tu[-1]
                                if t == first:
                                    comNum[i - orderNumberF * 2][t][0] = bNum

                                    bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                    tempPool = []  # 用于用于衔接两辆拖船

                                    tempBestj = []  # 用于临时记录拖船的单拖船最优方案
                                    tempBestway = []  # 用于临时记录拖船的单拖船最优方案
                                    tempBestloss = 0  # 用于临时记录拖船的单拖船最优方案
                                    tempBestpool = []  # 用于临时记录拖船的单拖船最优方案

                                    # 不考虑重用
                                    for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                        if haveAny1(i, ways[t]):
                                            if k != len(ways[t]):
                                                k += 1
                                                continue
                                        if k == len(ways[t]):
                                            pass
                                        else:
                                            if ways[t][k] < 2 * orderNumberF:
                                                if k % 2 == 0:
                                                    if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                                else:
                                                    if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                            elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderE2[ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass

                                        # print("k=", k)
                                        tempSolution = clone(ways[t])
                                        # print("ways[j]=", ways[j])
                                        # print("tempSolution=", tempSolution)
                                        tempSolution.insert(k, i)  # 在此位置插入终点
                                        # print("tempSolution=", tempSolution)
                                        # print("bNum=", bNum)
                                        needPoolAll = list(
                                            itertools.combinations(pool1, bNum))  # 从pool1中取出needNumber个数
                                        # print("needPoolAll=", needPoolAll)
                                        for p in needPoolAll:
                                            needPools = list(sorted(p, key=lambda i: orderE2[
                                                i - 2 * orderNumberF - orderNumberE1, 9]))
                                            tempBracket = []
                                            tempBracket.append(needPools)
                                            needPools = tempBracket
                                            # if bNum==8:
                                            #     print(needPools)
                                            # if needPools==[[31,32,33,34,35,36,37,38]]:
                                            #     print("right here!")
                                            for needPool in needPools:
                                                # print("needPool=", needPool)
                                                for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                    if haveAny2(i, tempSolution):
                                                        if pos != k:
                                                            pos += 1
                                                            continue
                                                    if pos == k:
                                                        pass
                                                    else:
                                                        if tempSolution[pos] < 2 * orderNumberF:
                                                            if tempSolution[pos] % 2 == 0:
                                                                if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    # print(pos)
                                                                    pass
                                                            else:
                                                                if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    pass
                                                        elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                            if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderE2[tempSolution[
                                                                           pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass

                                                    tempNeedPool = clone(needPool)
                                                    tempTempSolution = clone(tempSolution)
                                                    tempK = k
                                                    tempTempSolution.insert(pos,
                                                                            tempNeedPool[0])  # 插入pool中的第一个元素
                                                    # print("tempTempSolution=",tempTempSolution)
                                                    tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                    tempK += 1
                                                    if tempNeedPool != []:
                                                        cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                                  needPool)
                                                    else:
                                                        temp = []
                                                        for nub in range(courierNumber):
                                                            temp.append([])
                                                        temp[t] = tempTempSolution
                                                        tempLoss = lossFunction(temp, startTime, printf=False)
                                                        if (tempLoss < bestLossE2):
                                                            # print("bestlossE2=",bestLossE2)
                                                            # print("tempLoss=", tempLoss)

                                                            bestLossE2 = tempLoss
                                                            bestJ = t
                                                            bestWay = clone(tempTempSolution)
                                                            bestPool = clone(needPool)

                                    tempsolution = clone(ways)
                                    tempsolution[bestJ] = bestWay

                                    comNum[i - orderNumberF * 2][t][0] = bNum

                                    tempBestj.append(bestJ)  # t拖船的单拖船最优方案
                                    tempBestway.append(bestWay)  # t拖船的单拖船最优方案
                                    tempBestpool.append(bestPool)  # t拖船的单拖船最优方案

                                    tempPool = clone(pool1)
                                    for tim in range(len(tempBestj)):
                                        for point in tempBestpool[tim]:
                                            tempPoint = point
                                            tempPool.remove(tempPoint)

                                    continue
                                elif t == last:
                                    comNum[i - orderNumberF * 2][t][0] = numberTug
                                    # print("comNum=", comNum)

                                    bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                    for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                        # print("k=", k)
                                        if haveAny1(i, ways[t]):
                                            if k != len(ways[t]):
                                                k += 1
                                                continue
                                        if k == len(ways[t]):
                                            pass
                                        else:
                                            if ways[t][k] < 2 * orderNumberF:
                                                if k % 2 == 0:
                                                    if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                                else:
                                                    if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                            elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderE2[
                                                    ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                        orderE1[
                                                            i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass

                                        tempSolution = clone(ways[t])
                                        # print("ways[t]=", ways[t])
                                        # print("tempSolution=", tempSolution)
                                        tempSolution.insert(k, i)  # 在此位置插入终点
                                        # print("tempSolution=", tempSolution)
                                        # print("numberTug=", numberTug)
                                        needPoolAll = list(
                                            itertools.combinations(tempPool,
                                                                   numberTug))  # 从pool1中取出needNumber个数
                                        # print("needPoolAll=", needPoolAll)
                                        for p in needPoolAll:
                                            needPools = list(sorted(p, key=lambda i: orderE2[
                                                i - 2 * orderNumberF - orderNumberE1, 9]))
                                            tempBracket = []
                                            tempBracket.append(needPools)
                                            needPools = tempBracket

                                            for needPool in needPools:
                                                # print("needPool=", needPool)
                                                for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                    if haveAny2(i, tempSolution):
                                                        if pos != k:
                                                            pos += 1
                                                            continue
                                                    if pos == k:
                                                        pass
                                                    else:
                                                        if tempSolution[pos] < 2 * orderNumberF:
                                                            if tempSolution[pos] % 2 == 0:
                                                                if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    # print(pos)
                                                                    pass
                                                            else:
                                                                if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    pass
                                                        elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                            if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderE2[tempSolution[
                                                                           pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass

                                                    tempNeedPool = clone(needPool)
                                                    tempTempSolution = clone(tempSolution)
                                                    tempK = k
                                                    tempTempSolution.insert(pos,
                                                                            tempNeedPool[0])  # 插入pool中的第一个元素
                                                    # print("tempTempSolution=",tempTempSolution)
                                                    tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                    tempK += 1
                                                    if tempNeedPool != []:
                                                        cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                                  needPool)
                                                    else:
                                                        temp = []
                                                        for nub in range(courierNumber):
                                                            temp.append([])
                                                        temp[t] = tempTempSolution
                                                        tempLoss = lossFunction(temp, startTime, printf=False)
                                                        if (tempLoss < bestLossE2):
                                                            # print("bestlossE2=",bestLossE2)
                                                            # print("tempLoss=", tempLoss)

                                                            bestLossE2 = tempLoss
                                                            bestJ = t
                                                            bestWay = clone(tempTempSolution)
                                                            bestPool = clone(needPool)

                                    tempsolution = clone(ways)

                                    for timm in range(len(tempBestj)):
                                        tempsolution[tempBestj[timm]] = tempBestway[timm]

                                    tempsolution[bestJ] = bestWay

                                    comNum[i - orderNumberF * 2][t][0] = numberTug

                                    tempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                    tempBestj.append(bestJ)
                                    tempBestway.append(bestWay)
                                    tempBestpool.append(bestPool)

                                    if ti == 0:
                                        bTempBestloss = tempBestloss  # 两拖船不重用的最优解
                                        bTempBestj = tempBestj  # 两拖船不重用的最优解
                                        bTempBestway = tempBestway  # 两拖船不重用的最优解
                                        bTempBestpool = tempBestpool  # 两拖船不重用的最优解
                                        ti += 1
                                    elif ti >= 1:
                                        if tempBestloss < bTempBestloss:
                                            bTempBestloss = tempBestloss  # 两拖船不重用的最优解
                                            bTempBestj = tempBestj  # 两拖船不重用的最优解
                                            bTempBestway = tempBestway  # 两拖船不重用的最优解
                                            bTempBestpool = tempBestpool  # 两拖船不重用的最优解
                                        ti += 1
                if bTempBestloss < BestLoss:
                    BestLoss = bTempBestloss
                    BestJ = bTempBestj
                    BestWay = bTempBestway
                    BestPool = bTempBestpool

            # 双拖船重用
            elif needNumber <= 3 * maxcap:
                tugNum = 2

                numberTug = 0  # 用于记录第二个拖船需要拉多少个驳船
                nTug1 = 0
                nTug2 = 0

                tugAll = list(itertools.combinations(tugs, tugNum))
                # print("tugAll=",tugAll)
                for n in tugAll:
                    tug = list(itertools.permutations(n))
                    # print("tug=",tug)
                    for tu in tug:
                        # print("tu=",tu)
                        # print("needNumber=",needNumber)
                        for bargeNum in range(needNumber):
                            # print("bargeNum=",bargeNum)
                            numberTug = needNumber  # numberTug是一个全局变量
                            bNum = bargeNum + 1  # bNum是第一艘拖船拉空驳船的数量
                            # print("bNum=",bNum)
                            numberTug -= bNum  # numberTug是现在还有多少空驳船需要拉
                            # print("numberTug=",numberTug)

                            if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (bNum == 1) or (bNum == 2):
                                # print("break")
                                continue

                            for t in tu:
                                first = tu[0]
                                last = tu[-1]
                                if t == first:

                                    bTempBestloss = 0  # 第一辆拖船的部分最优解
                                    bTempBestj = []  # 第一辆拖船的部分最优解
                                    bTempBestway = []  # 第一辆拖船的部分最优解
                                    bTempBestpool = []  # 第一辆拖船的部分最优解

                                    comNum[i - orderNumberF * 2][t][0] = bNum
                                    # print("comNum=", comNum)
                                    # print("t=",t)

                                    bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                    tempPool = []  # 用于用于衔接两辆拖船

                                    tempBestj = []  # 用于临时记录拖船的单拖船最优方案
                                    tempBestway = []  # 用于临时记录拖船的单拖船最优方案
                                    tempBestloss = 0  # 用于临时记录拖船的单拖船最优方案
                                    tempBestpool = []  # 用于临时记录拖船的单拖船最优方案

                                    # 不考虑重用
                                    for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                        if haveAny1(i, ways[t]):
                                            if k != len(ways[t]):
                                                k += 1
                                                continue
                                        if k == len(ways[t]):
                                            pass
                                        else:
                                            if ways[t][k] < 2 * orderNumberF:
                                                if k % 2 == 0:
                                                    if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                                else:
                                                    if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                            elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderE2[ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass

                                        # print("k=", k)
                                        tempSolution = clone(ways[t])
                                        # print("ways[j]=", ways[j])
                                        # print("tempSolution=", tempSolution)
                                        tempSolution.insert(k, i)  # 在此位置插入终点
                                        # print("tempSolution=", tempSolution)
                                        # print("bNum=", bNum)
                                        needPoolAll = list(
                                            itertools.combinations(pool1, bNum))  # 从pool1中取出needNumber个数
                                        # print("needPoolAll=", needPoolAll)
                                        for p in needPoolAll:
                                            needPools = list(sorted(p, key=lambda i: orderE2[
                                                i - 2 * orderNumberF - orderNumberE1, 9]))
                                            tempBracket = []
                                            tempBracket.append(needPools)
                                            needPools = tempBracket
                                            # if bNum==8:
                                            #     print(needPools)
                                            # if needPools==[[31,32,33,34,35,36,37,38]]:
                                            #     print("right here!")
                                            for needPool in needPools:
                                                # print("needPool=", needPool)
                                                for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                    if haveAny2(i, tempSolution):
                                                        if pos != k:
                                                            pos += 1
                                                            continue
                                                    if pos == k:
                                                        pass
                                                    else:
                                                        if tempSolution[pos] < 2 * orderNumberF:
                                                            if tempSolution[pos] % 2 == 0:
                                                                if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    # print(pos)
                                                                    pass
                                                            else:
                                                                if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    pass
                                                        elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                            if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderE2[tempSolution[
                                                                           pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass

                                                    tempNeedPool = clone(needPool)
                                                    tempTempSolution = clone(tempSolution)
                                                    tempK = k
                                                    tempTempSolution.insert(pos,
                                                                            tempNeedPool[0])  # 插入pool中的第一个元素
                                                    # print("tempTempSolution=",tempTempSolution)
                                                    tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                    tempK += 1
                                                    if tempNeedPool != []:
                                                        cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                                  needPool)
                                                    else:
                                                        temp = []
                                                        for nub in range(courierNumber):
                                                            temp.append([])
                                                        temp[t] = tempTempSolution
                                                        tempLoss = lossFunction(temp, startTime, printf=False)
                                                        if (tempLoss < bestLossE2):
                                                            # print("bestlossE2=",bestLossE2)
                                                            # print("tempLoss=", tempLoss)

                                                            bestLossE2 = tempLoss
                                                            bestJ = t
                                                            bestWay = clone(tempTempSolution)
                                                            bestPool = clone(needPool)

                                    tempsolution = clone(ways)
                                    tempsolution[bestJ] = bestWay

                                    comNum[i - orderNumberF * 2][t][0] = bNum

                                    tempBestloss = lossFunction(tempsolution, startTime, printf=False)  # t拖船的单拖船最优方案
                                    tempBestj.append(bestJ)  # t拖船的单拖船最优方案
                                    tempBestway.append(bestWay)  # t拖船的单拖船最优方案
                                    tempBestpool.append(bestPool)  # t拖船的单拖船最优方案

                                    bTempBestloss = tempBestloss  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                    bTempBestj = tempBestj  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                    bTempBestway = tempBestway  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                    bTempBestpool = tempBestpool  # 把t拖船的第一次得到的单拖船最优方案存在这里面

                                    bestTempTempBestloss = float('inf')  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                    bestTempTempBestj = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                    bestTempTempBestway = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                    bestTempTempBestpool = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较

                                    ti = 0  # 用于记录是第几次得到了tempTempBestloss

                                    # 考虑重用
                                    for needM in range(bNum):
                                        nM = needM + 1  # 第一辆拖船第一次访问拉的驳船数
                                        nTug1 = bNum
                                        nTug1 -= nM  # 第一辆拖船第二次访问拉的驳船数
                                        if (nTug1 == 0) or (nTug1 == 1) or (nM == 1):
                                            continue
                                        for timess in range(maxTravelTime):

                                            if timess == 0:  # 考虑第一次访问的情况

                                                tempTempBestloss = 0  # 临时记录第一辆拖船的重用最优解
                                                tempTempBestj = []  # 临时记录第一辆拖船的重用最优解
                                                tempTempBestway = []  # 临时记录第一辆拖船的重用最优解
                                                tempTempBestpool = []  # 临时记录第一辆拖船的重用最优解

                                                bestJ = 0  # 临时存储一下局部最优解
                                                bestWay = []  # 临时存储一下局部最优解
                                                bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                                bestPool = []  # 临时存储一下局部最优解

                                                comNum[i - orderNumberF * 2][t][timess] = nTug1

                                                tempPooll = []  # 用于衔接两次访问

                                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                                    # print("k=", k)
                                                    if haveAny1(i, ways[t]):
                                                        if k != len(ways[t]):
                                                            k += 1
                                                            continue
                                                    if k == len(ways[t]):
                                                        pass
                                                    else:
                                                        if ways[t][k] < 2 * orderNumberF:
                                                            if k % 2 == 0:
                                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                                    continue
                                                                else:
                                                                    pass
                                                            else:
                                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                                    continue
                                                                else:
                                                                    pass
                                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderE2[
                                                                ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                    orderE1[
                                                                        i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass

                                                    tempSolution = clone(ways[t])
                                                    # print("ways[j]=", ways[t])
                                                    # print("tempSolution=", tempSolution)
                                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                                    # print("tempSolution=", tempSolution)
                                                    needPoolAll = list(
                                                        itertools.combinations(pool1, nTug1))  # 从pool1中取出needNumber个数

                                                    # print("needPoolAll=", needPoolAll)
                                                    for p in needPoolAll:
                                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                                        tempBracket = []
                                                        tempBracket.append(needPools)
                                                        needPools = tempBracket
                                                        for needPool in needPools:
                                                            # print("needPool=", needPool)
                                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                                if haveAny2(i, tempSolution):
                                                                    if pos != k:
                                                                        pos += 1
                                                                        continue
                                                                if pos == k:
                                                                    pass
                                                                else:
                                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                                        if tempSolution[pos] % 2 == 0:
                                                                            if orderF[int((tempSolution[pos]) / 2), 5] < \
                                                                                    orderE2[
                                                                                        needPool[
                                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                                continue
                                                                            else:
                                                                                # print(pos)
                                                                                pass
                                                                        else:
                                                                            if orderF[int((tempSolution[pos]) / 2), 7] < \
                                                                                    orderE2[
                                                                                        needPool[
                                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                                continue
                                                                            else:
                                                                                pass
                                                                    elif tempSolution[
                                                                        pos] < 2 * orderNumberF + orderNumberE1:
                                                                        if orderE1[
                                                                            tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass
                                                                    else:
                                                                        if orderE2[tempSolution[
                                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass

                                                                tempNeedPool = clone(needPool)
                                                                tempTempSolution = clone(tempSolution)
                                                                tempK = k
                                                                tempTempSolution.insert(pos, tempNeedPool[
                                                                    0])  # 插入pool中的第一个元素
                                                                # print("tempTempSolution=",tempTempSolution)
                                                                tempNeedPool.remove(
                                                                    tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                                tempK += 1
                                                                if tempNeedPool != []:
                                                                    cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                              pos, t, needPool)
                                                                else:
                                                                    temp = []
                                                                    for nub in range(courierNumber):
                                                                        temp.append([])
                                                                    temp[t] = tempTempSolution
                                                                    tempLoss = lossFunction(temp, startTime,
                                                                                            printf=False)
                                                                    # print("tempLoss=", tempLoss)

                                                                    if (tempLoss < bestLossE2):
                                                                        # print("bestlossE2=", bestLossE2)
                                                                        # print("tempLoss=", tempLoss)

                                                                        bestLossE2 = tempLoss
                                                                        bestJ = t
                                                                        bestWay = clone(tempTempSolution)
                                                                        bestPool = clone(needPool)

                                                tempTempBestj.append(bestJ)
                                                tempTempBestway.append(bestWay)
                                                tempTempBestpool.append(bestPool)

                                                tempPooll = clone(pool1)
                                                for point in bestPool:
                                                    tempPoint = point
                                                    tempPooll.remove(tempPoint)

                                                continue

                                            if timess == 1:  # 考虑第二次访问的情况

                                                comNum[i - orderNumberF * 2][t][timess] = nM

                                                bestJ = 0  # 临时存储一下局部最优解
                                                bestWay = []  # 临时存储一下局部最优解
                                                bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                                bestPool = []  # 临时存储一下局部最优解

                                                for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                                    # print("k=", k)
                                                    couldPos = tempTempBestway[0].index(i)
                                                    if k <= couldPos:
                                                        continue
                                                    tempSolution = clone(tempTempBestway[0])
                                                    # print("ways[t]=", ways[t])
                                                    # print("tempSolution=", tempSolution)
                                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                                    # print("tempSolution=", tempSolution)
                                                    # print("numberTug=", numberTug)
                                                    needPoolAll = list(
                                                        itertools.combinations(tempPooll,
                                                                               nM))  # 从pool1中取出needNumber个数
                                                    # print("needPoolAll=", needPoolAll)
                                                    for p in needPoolAll:
                                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                                        tempBracket = []
                                                        tempBracket.append(needPools)
                                                        needPools = tempBracket

                                                        for needPool in needPools:
                                                            # print("needPool=", needPool)
                                                            for pos in range(couldPos + 1,
                                                                             k + 1):  # pos是pool中第一个元素要插入的位置
                                                                tempNeedPool = clone(needPool)
                                                                tempTempSolution = clone(tempSolution)
                                                                tempK = k
                                                                tempTempSolution.insert(pos, tempNeedPool[
                                                                    0])  # 插入pool中的第一个元素
                                                                # print("tempTempSolution=",tempTempSolution)
                                                                tempNeedPool.remove(
                                                                    tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                                tempK += 1
                                                                if tempNeedPool != []:
                                                                    cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                              pos, t,
                                                                              needPool)
                                                                else:
                                                                    temp = []
                                                                    for nub in range(courierNumber):
                                                                        temp.append([])
                                                                    temp[t] = tempTempSolution
                                                                    tempLoss = lossFunction(temp, startTime,
                                                                                            printf=False)
                                                                    if (tempLoss < bestLossE2):
                                                                        # print("bestlossE2=",bestLossE2)
                                                                        # print("tempLoss=", tempLoss)

                                                                        bestLossE2 = tempLoss
                                                                        bestJ = t
                                                                        bestWay = clone(tempTempSolution)
                                                                        bestPool = clone(needPool)

                                                tempsolution = clone(ways)
                                                tempsolution[bestJ] = bestWay

                                                comNum[i - orderNumberF * 2][t][0] = nTug1
                                                comNum[i - orderNumberF * 2][t][1] = nM

                                                tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                                tempTempBestj.append(bestJ)
                                                tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                                                tempTempBestpool.append(bestPool)
                                                if ti == 0:
                                                    bestTempTempBestloss = tempTempBestloss
                                                    bestTempTempBestj = tempTempBestj
                                                    bestTempTempBestway = tempTempBestway
                                                    bestTempTempBestpool = tempTempBestpool
                                                    ti += 1
                                                else:
                                                    if bestTempTempBestloss > tempTempBestloss:
                                                        bestTempTempBestloss = tempTempBestloss
                                                        bestTempTempBestj = tempTempBestj
                                                        bestTempTempBestway = tempTempBestway
                                                        bestTempTempBestpool = tempTempBestpool
                                                    ti += 1

                                    if bestTempTempBestloss < bTempBestloss:
                                        bTempBestloss = bestTempTempBestloss
                                        bTempBestj = bestTempTempBestj  # [a,b]
                                        bTempBestway = bestTempTempBestway  # [[a,b],[c,d]]
                                        bTempBestpool = bestTempTempBestpool  # [[a,b],[c,d]]

                                    tempPool = clone(pool1)
                                    for tim in range(len(bTempBestj)):
                                        for point in bTempBestpool[tim]:
                                            tempPoint = point
                                            tempPool.remove(tempPoint)

                                    continue

                                    # 至此第一个拖船的最优方案已经存在了bTempBestloss、bTempBestj、bTempBestway、bTempBestpool里面
                                    # 以及供第二辆拖船选择的驳船都已经存放在tempPool里面了

                                elif t == last:  # maxTugNum=2时才会进入这里
                                    # print("t=",t)#对于最后一个元素t来说，他只有一种选择

                                    # 不考虑重用
                                    comNum[i - orderNumberF * 2][t][0] = numberTug
                                    # print("comNum=", comNum)

                                    tempBestloss = 0  # 用于临时记录第二辆拖船不重用的最优解
                                    tempBestj = []  # 用于临时记录第二辆拖船不重用的最优解
                                    tempBestway = []  # 用于临时记录第二辆拖船不重用的最优解
                                    tempBestpool = []  # 用于临时记录第二辆拖船不重用的最优解

                                    tTempBestloss = 0  # 第二辆拖船的最优解
                                    tTempBestj = []  # 第二辆拖船的最优解
                                    tTempBestway = []  # 第二辆拖船的最优解
                                    tTempBestpool = []  # 第二辆拖船的最优解

                                    bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                    for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                        # print("k=", k)
                                        if haveAny1(i, ways[t]):
                                            if k != len(ways[t]):
                                                k += 1
                                                continue
                                        if k == len(ways[t]):
                                            pass
                                        else:
                                            if ways[t][k] < 2 * orderNumberF:
                                                if k % 2 == 0:
                                                    if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                                else:
                                                    if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                            elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderE2[
                                                    ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                        orderE1[
                                                            i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass

                                        tempSolution = clone(ways[t])
                                        # print("ways[t]=", ways[t])
                                        # print("tempSolution=", tempSolution)
                                        tempSolution.insert(k, i)  # 在此位置插入终点
                                        # print("tempSolution=", tempSolution)
                                        # print("numberTug=", numberTug)
                                        needPoolAll = list(
                                            itertools.combinations(tempPool,
                                                                   numberTug))  # 从pool1中取出needNumber个数
                                        # print("needPoolAll=", needPoolAll)
                                        for p in needPoolAll:
                                            needPools = list(sorted(p, key=lambda i: orderE2[
                                                i - 2 * orderNumberF - orderNumberE1, 9]))
                                            tempBracket = []
                                            tempBracket.append(needPools)
                                            needPools = tempBracket

                                            for needPool in needPools:
                                                # print("needPool=", needPool)
                                                for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                    if haveAny2(i, tempSolution):
                                                        if pos != k:
                                                            pos += 1
                                                            continue
                                                    if pos == k:
                                                        pass
                                                    else:
                                                        if tempSolution[pos] < 2 * orderNumberF:
                                                            if tempSolution[pos] % 2 == 0:
                                                                if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    # print(pos)
                                                                    pass
                                                            else:
                                                                if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    pass
                                                        elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                            if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderE2[tempSolution[
                                                                           pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass

                                                    tempNeedPool = clone(needPool)
                                                    tempTempSolution = clone(tempSolution)
                                                    tempK = k
                                                    tempTempSolution.insert(pos,
                                                                            tempNeedPool[0])  # 插入pool中的第一个元素
                                                    # print("tempTempSolution=",tempTempSolution)
                                                    tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                    tempK += 1
                                                    if tempNeedPool != []:
                                                        cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                                  needPool)
                                                    else:
                                                        temp = []
                                                        for nub in range(courierNumber):
                                                            temp.append([])
                                                        temp[t] = tempTempSolution
                                                        tempLoss = lossFunction(temp, startTime, printf=False)
                                                        if (tempLoss < bestLossE2):
                                                            # print("bestlossE2=",bestLossE2)
                                                            # print("tempLoss=", tempLoss)

                                                            bestLossE2 = tempLoss
                                                            bestJ = t
                                                            bestWay = clone(tempTempSolution)
                                                            bestPool = clone(needPool)

                                    tempsolution = clone(ways)

                                    for timm in range(len(bTempBestj)):  # bTempBestj、bTempBestway这些都是第一辆拖船的部分最优解
                                        tempsolution[bTempBestj[timm]] = bTempBestway[timm]

                                    tempsolution[bestJ] = bestWay

                                    comNum[i - orderNumberF * 2][t][0] = numberTug

                                    tempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                    tempBestj.append(bestJ)
                                    tempBestway.append(bestWay)
                                    tempBestpool.append(bestPool)

                                    tTempBestloss = tempBestloss  # 第二辆拖船的最优解
                                    tTempBestj = tempBestj  # 第二辆拖船的最优解
                                    tTempBestway = tempBestway  # 第二辆拖船的最优解
                                    tTempBestpool = tempBestpool  # 第二辆拖船的最优解

                                    bestTempTempBestloss = float("inf")  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                    bestTempTempBestj = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                    bestTempTempBestway = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                    bestTempTempBestpool = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较

                                    ti = 0

                                    # 考虑重用
                                    for needB in range(numberTug):
                                        nB = needB + 1  # 第一辆拖船第一次访问拉的驳船数
                                        nTug2 = numberTug
                                        nTug2 -= nB  # 第一辆拖船第二次访问拉的驳船数
                                        if (nTug2 == 0) or (nTug2 == 1) or (nB == 1):
                                            continue

                                        for timess in range(maxTravelTime):
                                            if timess == 0:  # 考虑第一次访问的情况

                                                tempTempBestloss = 0  # 用于临时记录第二辆拖船重用的最优解
                                                tempTempBestj = []  # 用于临时记录第二辆拖船重用的最优解
                                                tempTempBestway = []  # 用于临时记录第二辆拖船重用的最优解
                                                tempTempBestpool = []  # 用于临时记录第二辆拖船重用的最优解

                                                bestJ = 0  # 临时存储一下局部最优解
                                                bestWay = []  # 临时存储一下局部最优解
                                                bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                                bestPool = []  # 临时存储一下局部最优解

                                                comNum[i - orderNumberF * 2][t][timess] = nTug2

                                                ttempPool = []  # 用于衔接两次分配任务

                                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                                    # print("k=", k)
                                                    if haveAny1(i, ways[t]):
                                                        if k != len(ways[t]):
                                                            k += 1
                                                            continue
                                                    if k == len(ways[t]):
                                                        pass
                                                    else:
                                                        if ways[t][k] < 2 * orderNumberF:
                                                            if k % 2 == 0:
                                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                                    continue
                                                                else:
                                                                    pass
                                                            else:
                                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                                    continue
                                                                else:
                                                                    pass
                                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderE2[
                                                                ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                    orderE1[
                                                                        i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass

                                                    tempSolution = clone(ways[t])
                                                    # print("ways[j]=", ways[j])
                                                    # print("tempSolution=", tempSolution)
                                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                                    # print("tempSolution=", tempSolution)
                                                    # print("bNum=", bNum)
                                                    needPoolAll = list(
                                                        itertools.combinations(tempPool,
                                                                               nTug2))  # 从pool1中取出needNumber个数
                                                    # print("needPoolAll=", needPoolAll)
                                                    for p in needPoolAll:
                                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                                        tempBracket = []
                                                        tempBracket.append(needPools)
                                                        needPools = tempBracket

                                                        for needPool in needPools:
                                                            # print("needPool=", needPool)
                                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                                if haveAny2(i, tempSolution):
                                                                    if pos != k:
                                                                        pos += 1
                                                                        continue
                                                                if pos == k:
                                                                    pass
                                                                else:
                                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                                        if tempSolution[pos] % 2 == 0:
                                                                            if orderF[int((tempSolution[pos]) / 2), 5] < \
                                                                                    orderE2[
                                                                                        needPool[
                                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                                continue
                                                                            else:
                                                                                # print(pos)
                                                                                pass
                                                                        else:
                                                                            if orderF[int((tempSolution[pos]) / 2), 7] < \
                                                                                    orderE2[
                                                                                        needPool[
                                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                                continue
                                                                            else:
                                                                                pass
                                                                    elif tempSolution[
                                                                        pos] < 2 * orderNumberF + orderNumberE1:
                                                                        if orderE1[
                                                                            tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass
                                                                    else:
                                                                        if orderE2[tempSolution[
                                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass

                                                                tempNeedPool = clone(needPool)
                                                                tempTempSolution = clone(tempSolution)
                                                                tempK = k
                                                                tempTempSolution.insert(pos, tempNeedPool[
                                                                    0])  # 插入pool中的第一个元素
                                                                # print("tempTempSolution=",tempTempSolution)
                                                                tempNeedPool.remove(
                                                                    tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                                tempK += 1
                                                                if tempNeedPool != []:
                                                                    cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                              pos, t,
                                                                              needPool)
                                                                else:
                                                                    temp = []
                                                                    for nub in range(courierNumber):
                                                                        temp.append([])
                                                                    temp[t] = tempTempSolution
                                                                    tempLoss = lossFunction(temp, startTime,
                                                                                            printf=False)

                                                                    if (tempLoss < bestLossE2):
                                                                        # print("bestlossE2=",bestLossE2)
                                                                        # print("tempLoss=", tempLoss)

                                                                        bestLossE2 = tempLoss
                                                                        bestJ = t
                                                                        bestWay = clone(tempTempSolution)
                                                                        bestPool = clone(needPool)

                                                tempTempBestj.append(bestJ)
                                                tempTempBestway.append(bestWay)
                                                tempTempBestpool.append(bestPool)

                                                comNum[i - orderNumberF * 2][t][timess] = nTug2

                                                ttempPool = clone(tempPool)
                                                for point in bestPool:
                                                    tempPoint = point
                                                    ttempPool.remove(tempPoint)

                                                continue

                                            if timess == 1:  # 考虑第二次访问的情况

                                                comNum[i - orderNumberF * 2][t][timess] = nB

                                                bestJ = 0  # 临时存储一下局部最优解
                                                bestWay = []  # 临时存储一下局部最优解
                                                bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                                bestPool = []  # 临时存储一下局部最优解

                                                for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                                    couldPos = tempTempBestway[0].index(i)
                                                    if k <= couldPos:
                                                        continue
                                                    # print("k=", k)
                                                    tempSolution = clone(tempTempBestway[0])
                                                    # print("ways[t]=", ways[t])
                                                    # print("tempSolution=", tempSolution)
                                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                                    # print("tempSolution=", tempSolution)
                                                    # print("numberTug=", numberTug)
                                                    needPoolAll = list(
                                                        itertools.combinations(ttempPool,
                                                                               nB))  # 从pool1中取出needNumber个数
                                                    # print("needPoolAll=", needPoolAll)
                                                    for p in needPoolAll:
                                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                                        tempBracket = []
                                                        tempBracket.append(needPools)
                                                        needPools = tempBracket

                                                        for needPool in needPools:
                                                            # print("needPool=", needPool)
                                                            for pos in range(couldPos + 1,
                                                                             k + 1):  # pos是pool中第一个元素要插入的位置
                                                                tempNeedPool = clone(needPool)
                                                                tempTempSolution = clone(tempSolution)
                                                                tempK = k
                                                                tempTempSolution.insert(pos, tempNeedPool[
                                                                    0])  # 插入pool中的第一个元素
                                                                # print("tempTempSolution=",tempTempSolution)
                                                                tempNeedPool.remove(
                                                                    tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                                tempK += 1
                                                                if tempNeedPool != []:
                                                                    cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                              pos, t,
                                                                              needPool)
                                                                else:
                                                                    temp = []
                                                                    for nub in range(courierNumber):
                                                                        temp.append([])
                                                                    temp[t] = tempTempSolution
                                                                    tempLoss = lossFunction(temp, startTime,
                                                                                            printf=False)
                                                                    if (tempLoss < bestLossE2):
                                                                        # print("bestlossE2=",bestLossE2)
                                                                        # print("tempLoss=", tempLoss)

                                                                        bestLossE2 = tempLoss
                                                                        bestJ = t
                                                                        bestWay = clone(tempTempSolution)
                                                                        bestPool = clone(needPool)

                                                tempsolution = clone(ways)
                                                tempsolution[bestJ] = bestWay

                                                comNum[i - orderNumberF * 2][t][0] = nTug2
                                                comNum[i - orderNumberF * 2][t][1] = nB

                                                tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                                tempTempBestj.append(bestJ)
                                                tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                                                tempTempBestpool.append(bestPool)

                                                if ti == 0:
                                                    bestTempTempBestloss = tempTempBestloss
                                                    bestTempTempBestj = tempTempBestj
                                                    bestTempTempBestway = tempTempBestway
                                                    bestTempTempBestpool = tempTempBestpool
                                                    ti += 1
                                                else:
                                                    if bestTempTempBestloss > tempTempBestloss:
                                                        bestTempTempBestloss = tempTempBestloss
                                                        bestTempTempBestj = tempTempBestj
                                                        bestTempTempBestway = tempTempBestway
                                                        bestTempTempBestpool = tempTempBestpool
                                                    ti += 1

                                    if bestTempTempBestloss < tTempBestloss:
                                        tTempBestloss = bestTempTempBestloss
                                        tTempBestj = bestTempTempBestj  # [a,b]
                                        tTempBestway = bestTempTempBestway  # [[a,b],[c,d]]
                                        tTempBestpool = bestTempTempBestpool  # [[a,b],[c,d]]

                                    # 至此，第一辆拖船以及第二辆拖船的最优部分解均已经收集完毕

                                    for leng in range(len(tTempBestj)):
                                        bTempBestj.append(tTempBestj[leng])
                                        bTempBestway.append(tTempBestway[leng])
                                        bTempBestpool.append(tTempBestpool[leng])

                                    tempsolution = clone(ways)

                                    for leng in range(len(bTempBestj)):
                                        tempsolution[bTempBestj[leng]] = bTempBestway[leng]

                                    tNum = []

                                    for ii in range(orderNumberE1):
                                        tNum.append([])
                                        for jj in range(courierNumber):
                                            tNum[ii].append([])
                                            for _ in range(2):
                                                tNum[ii][jj].append(0)

                                    comNum[i - orderNumberF * 2] = tNum[i - orderNumberF * 2]

                                    tempp = []
                                    for to in bTempBestj:
                                        if to in tempp:
                                            # 查找 to 在 bbestTempBestj 中第二次出现的位置
                                            index = find_nth_occurrence(bTempBestj, to, 2)
                                            if index != -1:
                                                comNum[i - orderNumberF * 2][to][1] = len(bTempBestpool[index])
                                        else:
                                            index = bTempBestj.index(to)
                                            comNum[i - orderNumberF * 2][to][0] = len(bTempBestpool[index])
                                        tempp.append(to)

                                    tempBestloss = lossFunction(tempsolution, startTime, printf=False)

                                    if tii == 0:
                                        bbestTempBestloss = tempBestloss
                                        bbestTempBestj = bTempBestj
                                        bbestTempBestway = bTempBestway
                                        bbestTempBestpool = bTempBestpool
                                        tii += 1
                                    elif tii != 0:
                                        tii += 1
                                        if bbestTempBestloss >= tempBestloss:
                                            bbestTempBestloss = tempBestloss
                                            bbestTempBestj = bTempBestj
                                            bbestTempBestway = bTempBestway
                                            bbestTempBestpool = bTempBestpool
                    if bbestTempBestloss < BestLoss:
                        BestLoss = bbestTempBestloss
                        BestJ = bbestTempBestj
                        BestWay = bbestTempBestway
                        BestPool = bbestTempBestpool

            for chosenone in range(len(BestJ)):
                ways[BestJ[chosenone]] = BestWay[chosenone]
                for point in BestPool[chosenone]:
                    tempPoint = point
                    pool1.remove(tempPoint)
            tNum = []

            for ii in range(orderNumberE1):
                tNum.append([])
                for jj in range(courierNumber):
                    tNum[ii].append([])
                    for _ in range(2):
                        tNum[ii][jj].append(0)

            comNum[i - orderNumberF * 2] = tNum[i - orderNumberF * 2]

            tempp = []
            for to in BestJ:
                if to in tempp:
                    # 查找 to 在 bbestTempBestj 中第二次出现的位置
                    index = find_nth_occurrence(BestJ, to, 2)
                    if index != -1:
                        comNum[i - orderNumberF * 2][to][1] = len(BestPool[index])
                else:
                    index = BestJ.index(to)
                    comNum[i - orderNumberF * 2][to][0] = len(BestPool[index])
                tempp.append(to)

    pool1 = []
    pool2 = []
    return ways,pool1,pool2


# 自适应规模-遗憾贪婪修复E类型订单的某路径
def repairE3(ways, pool1, pool2):  # 贪婪重建算子
    global comNum  # 用于记录每个拖船在完成相应的E类型时第一次（如果有第二次的话）拉了几个空驳船

    global tugs  # 一会儿用它结合lossFunction操控拖船

    global bestJ  # 调用递归函数时要用
    global bestWay  # 调用递归函数时要用
    global bestLossE2  # 调用递归函数时要用
    global bestPool  # 调用递归函数时要用

    global numberTug  # 用于记录tugNum等于1时第二次访问拖船需要拉多少个驳船，以及tugNum等于二时第二艘拖船需要拉多少个驳船
    global nTug1  # 用于记录tugNum等于2时第一个拖船第二次访问拖船需要拉几个驳船
    global nTug2  # 用于记录tugNum等于2时第二个拖船第二次访问拖船需要拉几个驳船

    global maxTugNum  # 最多允许几个拖船共同完成一个E类型订单任务（默认为2
    global maxTravelTime  # 最多允许一个拖船访问同一个任务点几次（默认为2

    BestJJ = []
    BestWayy = []
    BestLosss = float("inf")
    BestPooll = []
    BestI = 0
    timer = 0

    tempPool1=pool1
    tempPool2=copy.deepcopy(pool1)
    for i in pool2:
        # print("i=", i)
        # print("pool1=", pool1)
        # print("pool2=", pool2)
        # print("ways=", ways)
        needNumber = int(orderE1[i - orderNumberF * 2][8])  # 获取E类型订单需要的空驳船数量
        pool1 = pool1[:needNumber]

        tii = 0  # 记录是第几次得到了临时最后解
        bbestTempBestloss = float("inf")  # 临时记录最后解
        bbestTempBestj = []  # 临时记录最后解
        bbestTempBestway = []  # 临时记录最后解
        bbestTempBestpool = []  # 临时记录最后解
        bbestTempBestlosss = float("inf")

        BestJ = []  # 记录单拖船不重用的最优解，记录最后的最优解
        BestWay = []  # 记录单拖船不重用的最优解，记录最后的最优解
        BestLoss = float('inf')  # 记录单拖船不重用的最优解，记录最后的最优解
        BestPool = []  # 记录单拖船不重用的最优解，记录最后的最优解

        bestj = []  # 临时存储一下单拖船不重用整体最优解
        bestway = []  # 临时存储一下单拖船不重用整体最优解
        bestloss = float('inf')  # 临时存储一下单拖船不重用整体最优解
        bestpool = []  # 临时存储一下单拖船不重用整体最优解

        bestlosss=float("inf")

        # 单拖船不重用
        if needNumber <= maxcap:
            tugNum = 1
            for j in range(len(ways)):  # 选择拖船
                bestJ = 0  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                bestWay = []  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                bestLossE2 = float('inf')  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                bestPool = []  # 因为要调用递归函数，所以在这里初始化这四个全局变量

                comNum[i - orderNumberF * 2][j][0] = needNumber

                for k in range(len(ways[j]) + 1):  # 选择终点插入位置
                    if haveAny1(i, ways[j]):
                        if k != len(ways[j]):
                            k += 1
                            continue
                    if k == len(ways[j]):
                        pass
                    else:
                        if ways[j][k] < 2 * orderNumberF:
                            if k % 2 == 0:
                                if orderF[int((ways[j][k]) / 2), 5] < orderE1[i - 2 * orderNumberF, 7]:
                                    continue
                                else:
                                    pass
                            else:
                                if orderF[int((ways[j][k]) / 2), 7] < orderE1[i - 2 * orderNumberF, 7]:
                                    continue
                                else:
                                    pass
                        elif ways[j][k] < 2 * orderNumberF + orderNumberE1:
                            if orderE1[ways[j][k] - 2 * orderNumberF, 7] < orderE1[i - 2 * orderNumberF, 7]:
                                continue
                            else:
                                pass
                        else:
                            if orderE2[ways[j][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                i - 2 * orderNumberF, 7]:
                                continue
                            else:
                                pass
                    tempSolution = clone(ways[j])
                    # print("ways[j]=", ways[j])
                    # print("tempSolution=", tempSolution)
                    tempSolution.insert(k, i)  # 在此位置插入终点
                    # print("tempSolution=", tempSolution)
                    needPoolAll = list(itertools.combinations(pool1, needNumber))  # 从pool1中取出needNumber个数
                    for p in needPoolAll:
                        # print("p=", p)
                        needPools = list(sorted(p, key=lambda i: orderE2[
                            i - 2 * orderNumberF - orderNumberE1, 9]))
                        tempBracket = []
                        tempBracket.append(needPools)
                        needPools = tempBracket

                        for needPool in needPools:
                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                if haveAny2(i, tempSolution):
                                    if pos != k:
                                        pos += 1
                                        continue
                                if pos == k:
                                    pass
                                else:
                                    if tempSolution[pos] < 2 * orderNumberF:
                                        if tempSolution[pos] % 2 == 0:
                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                continue
                                            else:
                                                # print(pos)
                                                pass
                                        else:
                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                continue
                                            else:
                                                pass
                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < orderE2[
                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                            continue
                                        else:
                                            pass
                                    else:
                                        if orderE2[tempSolution[pos] - 2 * orderNumberF - orderNumberE1, 9] < orderE2[
                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                            continue
                                        else:
                                            pass
                                tempNeedPool = clone(needPool)
                                tempTempSolution = clone(tempSolution)
                                tempK = k
                                tempTempSolution.insert(pos, tempNeedPool[0])  # 插入pool中的第一个元素
                                # print("tempTempSolution=",tempTempSolution)
                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                tempK += 1
                                if tempNeedPool != []:
                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, j, needPool)
                                else:
                                    temp = []
                                    for nub in range(courierNumber):
                                        temp.append([])
                                    temp[j] = tempTempSolution
                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                    if (tempLoss < bestLossE2):
                                        # print("bestlossE2=",bestLossE2)
                                        # print("tempLoss=", tempLoss)

                                        bestLossE2 = tempLoss
                                        bestJ = j
                                        bestWay = clone(tempTempSolution)
                                        bestPool = clone(needPool)
                                        # print("bestLossE2=", bestLossE2)
                                        # print("bestPool=", bestPool)
                                        # print("bestWay=", bestWay)
                                        # print("bestJ=", bestJ)

                tempsolution = clone(ways)
                tempsolution[bestJ] = bestWay
                # print("bestWay=",bestWay)
                if (j == 0):
                    # print("j=",j)
                    bestloss = lossFunction(tempsolution, startTime, printf=False)
                    # print("bestloss=", bestloss)
                    bestj = bestJ
                    bestway = bestWay
                    bestpool = bestPool
                elif j==1:
                    tempLoss=lossFunction(tempsolution, startTime, printf=False)
                    if tempLoss<bestloss:
                        bestlosss=bestloss

                        bestloss = tempLoss
                        bestj = bestJ
                        bestway = bestWay
                        bestpool = bestPool
                else:
                    # print("j=", j)
                    tempLoss = lossFunction(tempsolution, startTime, printf=False)
                    # print("tempLoss=", tempLoss)
                    if (tempLoss < bestlosss) and (tempLoss > bestloss):
                        bestlosss = tempLoss
                    elif (tempLoss < bestloss):
                        bestlosss=bestloss

                        bestloss = tempLoss
                        bestj = bestJ
                        bestway = bestWay
                        bestpool = bestPool

            BestLoss = bestlosss-bestloss  # 记录单拖船不重用的最优解，记录最后的最优解
            BestJ.append(bestj)  # 记录单拖船不重用的最优解，记录最后的最优解
            BestWay.append(bestway)  # 记录单拖船不重用的最优解，记录最后的最优解
            BestPool.append(bestpool)  # 记录单拖船不重用的最优解，记录最后的最优解

        # 单拖船重用+两拖船不重用
        elif needNumber <= 2 * maxcap:
            # 单拖船重用
            tugNum = 1
            bestTempBestloss = float("inf")  # 记录单拖船重用的最优解
            bestTempBestj = []  # 记录单拖船重用的最优解
            bestTempBestway = []  # 记录单拖船重用的最优解
            bestTempBestpool = []  # 记录单拖船重用的最优解
            bestTempBestlosss = float("inf")

            ti = 0  # 记录是第几次得到tempBestj、tempBestway、tempBestloss、tempBestpool了

            for j in range(len(ways)):  # 选择拖船
                # print("j=",j)
                for needN in range(needNumber):
                    numberTug = needNumber  # numberTug是一个全局变量
                    nN = needN + 1  # bNum是第一次访问拉空驳船的数量
                    # print("bNum=",bNum)
                    numberTug -= nN  # numberTug是第二次访问拉空驳船的数量
                    if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (nN == 1) or (
                            nN == 2):  # numberTug=0意味着第二次访问拉的数量为0，相当于只考虑了访问一次
                        continue

                    for times in range(maxTravelTime):
                        if times == 0:  # 考虑第一次访问的情况

                            tempTempBestloss = 0  # 临时存储第一次访问与第二次访问的解
                            tempTempBestj = []  # 临时存储第一次访问与第二次访问的解
                            tempTempBestway = []  # 临时存储第一次访问与第二次访问的解
                            tempTempBestpool = []  # 临时存储第一次访问与第二次访问的解

                            comNum[i - orderNumberF * 2][j][times] = numberTug

                            bestJ = 0  # 临时存储一下局部最优解
                            bestWay = []  # 临时存储一下局部最优解
                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                            bestPool = []  # 临时存储一下局部最优解

                            tempPool = []  # 用于衔接两次分配任务

                            for k in range(len(ways[j]) + 1):  # 选择终点插入位置
                                if haveAny1(i, ways[j]):
                                    if k != len(ways[j]):
                                        k += 1
                                        continue
                                if k == len(ways[j]):
                                    pass
                                else:
                                    if ways[j][k] < 2 * orderNumberF:
                                        if k % 2 == 0:
                                            if orderF[int((ways[j][k]) / 2), 5] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderF[int((ways[j][k]) / 2), 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                    elif ways[j][k] < 2 * orderNumberF + orderNumberE1:
                                        if orderE1[ways[j][k] - 2 * orderNumberF, 7] < orderE1[
                                            i - 2 * orderNumberF, 7]:
                                            continue
                                        else:
                                            pass
                                    else:
                                        if orderE2[ways[j][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                            i - 2 * orderNumberF, 7]:
                                            continue
                                        else:
                                            pass

                                tempSolution = clone(ways[j])
                                # print("ways[j]=", ways[j])
                                # print("tempSolution=", tempSolution)
                                tempSolution.insert(k, i)  # 在此位置插入终点
                                # print("tempSolution=", tempSolution)
                                # print("bNum=", bNum)
                                needPoolAll = list(
                                    itertools.combinations(pool1, numberTug))  # 从pool1中取出needNumber个数
                                # print("needPoolAll=", needPoolAll)
                                for p in needPoolAll:
                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                    tempBracket = []
                                    tempBracket.append(needPools)
                                    needPools = tempBracket
                                    for needPool in needPools:
                                        # print("needPool=", needPool)
                                        for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                            if haveAny2(i, tempSolution):
                                                if pos != k:
                                                    pos += 1
                                                    continue
                                            if pos == k:
                                                pass
                                            else:
                                                if tempSolution[pos] < 2 * orderNumberF:
                                                    if tempSolution[pos] % 2 == 0:
                                                        if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            # print(pos)
                                                            pass
                                                    else:
                                                        if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                    if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < orderE2[
                                                        needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                        continue
                                                    else:
                                                        pass
                                                else:
                                                    if orderE2[
                                                        tempSolution[pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                            orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                        continue
                                                    else:
                                                        pass

                                            tempNeedPool = clone(needPool)
                                            tempTempSolution = clone(tempSolution)
                                            tempK = k
                                            tempTempSolution.insert(pos, tempNeedPool[
                                                0])  # 插入pool中的第一个元素
                                            # print("tempTempSolution=",tempTempSolution)
                                            tempNeedPool.remove(
                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                            tempK += 1
                                            if tempNeedPool != []:
                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                          pos, j,
                                                          needPool)
                                            else:
                                                temp = []
                                                for nub in range(courierNumber):
                                                    temp.append([])
                                                temp[j] = tempTempSolution
                                                tempLoss = lossFunction(temp, startTime,
                                                                        printf=False)

                                                if (tempLoss < bestLossE2):
                                                    # print("bestlossE2=",bestLossE2)
                                                    # print("tempLoss=", tempLoss)

                                                    bestLossE2 = tempLoss
                                                    bestJ = j
                                                    bestWay = clone(tempTempSolution)
                                                    bestPool = clone(needPool)

                            tempTempBestj.append(bestJ)
                            tempTempBestway.append(bestWay)
                            tempTempBestpool.append(bestPool)

                            tempPool = clone(pool1)
                            for point in bestPool:
                                tempPoint = point
                                tempPool.remove(tempPoint)

                            continue

                        if times == 1:  # 考虑第二次访问的情况

                            comNum[i - orderNumberF * 2][j][times] = nN

                            bestJ = 0  # 临时存储一下局部最优解
                            bestWay = []  # 临时存储一下局部最优解
                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                            bestPool = []  # 临时存储一下局部最优解

                            for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                # print("k=", k)
                                couldPos = tempTempBestway[0].index(i)
                                if k <= couldPos:
                                    continue
                                tempSolution = clone(tempTempBestway[0])
                                # print("ways[t]=", ways[t])
                                # print("tempSolution=", tempSolution)
                                tempSolution.insert(k, i)  # 在此位置插入终点
                                # print("tempSolution=", tempSolution)
                                # print("numberTug=", numberTug)
                                needPoolAll = list(itertools.combinations(tempPool, nN))  # 从pool1中取出needNumber个数
                                # print("needPoolAll=", needPoolAll)
                                for p in needPoolAll:
                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                    tempBracket = []
                                    tempBracket.append(needPools)
                                    needPools = tempBracket

                                    for needPool in needPools:
                                        # print("needPool=", needPool)
                                        for pos in range(couldPos + 1, k + 1):  # pos是pool中第一个元素要插入的位置
                                            tempNeedPool = clone(needPool)
                                            tempTempSolution = clone(tempSolution)
                                            tempK = k
                                            tempTempSolution.insert(pos, tempNeedPool[0])  # 插入pool中的第一个元素
                                            # print("tempTempSolution=",tempTempSolution)
                                            tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                            tempK += 1
                                            if tempNeedPool != []:
                                                cirInsert(tempK, tempTempSolution, tempNeedPool, pos, j,
                                                          needPool)
                                            else:
                                                temp = []
                                                for nub in range(courierNumber):
                                                    temp.append([])
                                                temp[j] = tempTempSolution
                                                tempLoss = lossFunction(temp, startTime, printf=False)
                                                if (tempLoss < bestLossE2):
                                                    # print("bestlossE2=",bestLossE2)
                                                    # print("tempLoss=", tempLoss)

                                                    bestLossE2 = tempLoss
                                                    bestJ = j
                                                    bestWay = clone(tempTempSolution)
                                                    bestPool = clone(needPool)

                            tempsolution = clone(ways)
                            tempsolution[bestJ] = bestWay

                            comNum[i - orderNumberF * 2][j][0] = numberTug
                            comNum[i - orderNumberF * 2][j][1] = nN

                            tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                            tempTempBestj.append(bestJ)
                            tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                            tempTempBestpool.append(bestPool)

                            if ti == 0:
                                bestTempBestloss = tempTempBestloss
                                bestTempBestj = tempTempBestj
                                bestTempBestway = tempTempBestway
                                bestTempBestpool = tempTempBestpool
                                ti += 1
                            elif ti == 1:
                                if tempTempBestloss<bestTempBestloss:
                                    bestTempBestlosss = bestTempBestloss

                                    bestTempBestloss = tempTempBestloss
                                    bestTempBestj = tempTempBestj
                                    bestTempBestway = tempTempBestway
                                    bestTempBestpool = tempTempBestpool
                                else:
                                    bestTempBestlosss=tempTempBestloss
                                ti += 1
                            else:
                                if (bestTempBestlosss > tempTempBestloss) and (tempTempBestloss > bestTempBestloss):
                                    bestTempBestlosss=tempTempBestloss
                                elif (tempTempBestloss < bestTempBestloss):
                                    bestTempBestlosss = bestTempBestloss

                                    bestTempBestloss = tempTempBestloss
                                    bestTempBestj = tempTempBestj
                                    bestTempBestway = tempTempBestway
                                    bestTempBestpool = tempTempBestpool
                                ti += 1

            if bestTempBestloss < BestLoss:
                BestLoss = bestTempBestloss-bestTempBestlosss
                BestJ = bestTempBestj
                BestWay = bestTempBestway
                BestPool = bestTempBestpool

            # 两拖船不重用
            bTempBestloss = float("inf")  # 两拖船不重用的最优解
            bTempBestj = []  # 两拖船不重用的最优解
            bTempBestway = []  # 两拖船不重用的最优解
            bTempBestpool = []  # 两拖船不重用的最优解
            bTempBestlosss = float("inf")

            tugNum = 2
            ti = 0
            tugAll = list(itertools.combinations(tugs, tugNum))
            # print("tugAll=",tugAll)
            for n in tugAll:
                tug = list(itertools.permutations(n))
                # print("tug=",tug)
                for tu in tug:
                    # print("tu=",tu)
                    # print("needNumber=",needNumber)
                    for bargeNum in range(needNumber):
                        # print("bargeNum=",bargeNum)
                        numberTug = needNumber  # numberTug是一个全局变量
                        bNum = bargeNum + 1  # bNum是第一艘拖船拉空驳船的数量
                        # print("bNum=",bNum)
                        numberTug -= bNum  # numberTug是现在还有多少空驳船需要拉
                        # print("numberTug=",numberTug)

                        if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (bNum == 1) or (bNum == 2):
                            # print("break")
                            continue

                        for t in tu:
                            first = tu[0]
                            last = tu[-1]
                            if t == first:
                                comNum[i - orderNumberF * 2][t][0] = bNum

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                tempPool = []  # 用于用于衔接两辆拖船

                                tempBestj = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestway = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestloss = 0  # 用于临时记录拖船的单拖船最优方案
                                tempBestpool = []  # 用于临时记录拖船的单拖船最优方案

                                # 不考虑重用
                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    # print("k=", k)
                                    tempSolution = clone(ways[t])
                                    # print("ways[j]=", ways[j])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("bNum=", bNum)
                                    needPoolAll = list(
                                        itertools.combinations(pool1, bNum))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket
                                        # if bNum==8:
                                        #     print(needPools)
                                        # if needPools==[[31,32,33,34,35,36,37,38]]:
                                        #     print("right here!")
                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)
                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = bNum

                                tempBestj.append(bestJ)  # t拖船的单拖船最优方案
                                tempBestway.append(bestWay)  # t拖船的单拖船最优方案
                                tempBestpool.append(bestPool)  # t拖船的单拖船最优方案

                                tempPool = clone(pool1)
                                for tim in range(len(tempBestj)):
                                    for point in tempBestpool[tim]:
                                        tempPoint = point
                                        tempPool.remove(tempPoint)

                                continue
                            elif t == last:
                                comNum[i - orderNumberF * 2][t][0] = numberTug
                                # print("comNum=", comNum)

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    # print("k=", k)
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[
                                                ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                    orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    tempSolution = clone(ways[t])
                                    # print("ways[t]=", ways[t])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("numberTug=", numberTug)
                                    needPoolAll = list(
                                        itertools.combinations(tempPool,
                                                               numberTug))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket

                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)

                                for timm in range(len(tempBestj)):
                                    tempsolution[tempBestj[timm]] = tempBestway[timm]

                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = numberTug

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                tempBestj.append(bestJ)
                                tempBestway.append(bestWay)
                                tempBestpool.append(bestPool)

                                if ti == 0:
                                    bTempBestloss = tempBestloss  # 两拖船不重用的最优解
                                    bTempBestj = tempBestj  # 两拖船不重用的最优解
                                    bTempBestway = tempBestway  # 两拖船不重用的最优解
                                    bTempBestpool = tempBestpool  # 两拖船不重用的最优解
                                    ti += 1
                                elif ti == 1:
                                    if tempBestloss<bTempBestloss:
                                        bTempBestlosss=bTempBestloss

                                        bTempBestloss = tempBestloss  # 两拖船不重用的最优解
                                        bTempBestj = tempBestj  # 两拖船不重用的最优解
                                        bTempBestway = tempBestway  # 两拖船不重用的最优解
                                        bTempBestpool = tempBestpool  # 两拖船不重用的最优解
                                    else:
                                        bTempBestlosss = tempBestloss
                                    ti += 1
                                else:
                                    if (tempBestloss < bTempBestlosss) and (tempBestloss > bTempBestloss):
                                        bTempBestlosss = tempBestloss  # 两拖船不重用的最优解
                                    elif (tempBestloss<bTempBestloss):
                                        bTempBestlosss=bTempBestloss

                                        bTempBestloss = tempBestloss
                                        bTempBestj = tempBestj  # 两拖船不重用的最优解
                                        bTempBestway = tempBestway  # 两拖船不重用的最优解
                                        bTempBestpool = tempBestpool  # 两拖船不重用的最优解
                                    ti += 1
            if (bTempBestlosss-bTempBestloss) < BestLoss:
                BestLoss = bTempBestlosss - bTempBestloss
                BestJ = bTempBestj
                BestWay = bTempBestway
                BestPool = bTempBestpool

        # 双拖船重用
        elif needNumber <= 3 * maxcap:
            tugNum = 2

            numberTug = 0  # 用于记录第二个拖船需要拉多少个驳船
            nTug1 = 0
            nTug2 = 0

            tugAll = list(itertools.combinations(tugs, tugNum))
            # print("tugAll=",tugAll)
            for n in tugAll:
                tug = list(itertools.permutations(n))
                # print("tug=",tug)
                for tu in tug:
                    # print("tu=",tu)
                    # print("needNumber=",needNumber)
                    for bargeNum in range(needNumber):
                        # print("bargeNum=",bargeNum)
                        numberTug = needNumber  # numberTug是一个全局变量
                        bNum = bargeNum + 1  # bNum是第一艘拖船拉空驳船的数量
                        # print("bNum=",bNum)
                        numberTug -= bNum  # numberTug是现在还有多少空驳船需要拉
                        # print("numberTug=",numberTug)

                        if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (bNum == 1) or (bNum == 2):
                            # print("break")
                            continue

                        for t in tu:
                            first = tu[0]
                            last = tu[-1]
                            if t == first:

                                bTempBestloss = 0  # 第一辆拖船的部分最优解
                                bTempBestj = []  # 第一辆拖船的部分最优解
                                bTempBestway = []  # 第一辆拖船的部分最优解
                                bTempBestpool = []  # 第一辆拖船的部分最优解

                                comNum[i - orderNumberF * 2][t][0] = bNum
                                # print("comNum=", comNum)
                                # print("t=",t)

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                tempPool = []  # 用于用于衔接两辆拖船

                                tempBestj = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestway = []  # 用于临时记录拖船的单拖船最优方案
                                tempBestloss = 0  # 用于临时记录拖船的单拖船最优方案
                                tempBestpool = []  # 用于临时记录拖船的单拖船最优方案

                                # 不考虑重用
                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    # print("k=", k)
                                    tempSolution = clone(ways[t])
                                    # print("ways[j]=", ways[j])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("bNum=", bNum)
                                    needPoolAll = list(
                                        itertools.combinations(pool1, bNum))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket
                                        # if bNum==8:
                                        #     print(needPools)
                                        # if needPools==[[31,32,33,34,35,36,37,38]]:
                                        #     print("right here!")
                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)
                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = bNum

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)  # t拖船的单拖船最优方案
                                tempBestj.append(bestJ)  # t拖船的单拖船最优方案
                                tempBestway.append(bestWay)  # t拖船的单拖船最优方案
                                tempBestpool.append(bestPool)  # t拖船的单拖船最优方案

                                bTempBestloss = tempBestloss  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                bTempBestj = tempBestj  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                bTempBestway = tempBestway  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                bTempBestpool = tempBestpool  # 把t拖船的第一次得到的单拖船最优方案存在这里面

                                bestTempTempBestloss = float('inf')  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                bestTempTempBestj = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                bestTempTempBestway = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                bestTempTempBestpool = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较

                                ti = 0  # 用于记录是第几次得到了tempTempBestloss

                                # 考虑重用
                                for needM in range(bNum):
                                    nM = needM + 1  # 第一辆拖船第一次访问拉的驳船数
                                    nTug1 = bNum
                                    nTug1 -= nM  # 第一辆拖船第二次访问拉的驳船数
                                    if (nTug1 == 0) or (nTug1 == 1) or (nM == 1):
                                        continue
                                    for timess in range(maxTravelTime):

                                        if timess == 0:  # 考虑第一次访问的情况

                                            tempTempBestloss = 0  # 临时记录第一辆拖船的重用最优解
                                            tempTempBestj = []  # 临时记录第一辆拖船的重用最优解
                                            tempTempBestway = []  # 临时记录第一辆拖船的重用最优解
                                            tempTempBestpool = []  # 临时记录第一辆拖船的重用最优解

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            comNum[i - orderNumberF * 2][t][timess] = nTug1

                                            tempPooll = []  # 用于衔接两次访问

                                            for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                                # print("k=", k)
                                                if haveAny1(i, ways[t]):
                                                    if k != len(ways[t]):
                                                        k += 1
                                                        continue
                                                if k == len(ways[t]):
                                                    pass
                                                else:
                                                    if ways[t][k] < 2 * orderNumberF:
                                                        if k % 2 == 0:
                                                            if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                            i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[
                                                            ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass

                                                tempSolution = clone(ways[t])
                                                # print("ways[j]=", ways[t])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                needPoolAll = list(
                                                    itertools.combinations(pool1, nTug1))  # 从pool1中取出needNumber个数

                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket
                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            if haveAny2(i, tempSolution):
                                                                if pos != k:
                                                                    pos += 1
                                                                    continue
                                                            if pos == k:
                                                                pass
                                                            else:
                                                                if tempSolution[pos] < 2 * orderNumberF:
                                                                    if tempSolution[pos] % 2 == 0:
                                                                        if orderF[int((tempSolution[pos]) / 2), 5] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            # print(pos)
                                                                            pass
                                                                    else:
                                                                        if orderF[int((tempSolution[pos]) / 2), 7] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass
                                                                elif tempSolution[
                                                                    pos] < 2 * orderNumberF + orderNumberE1:
                                                                    if orderE1[
                                                                        tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass
                                                                else:
                                                                    if orderE2[tempSolution[
                                                                                   pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass

                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t, needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime,
                                                                                        printf=False)
                                                                # print("tempLoss=", tempLoss)

                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=", bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)
                                            tempTempBestpool.append(bestPool)

                                            tempPooll = clone(pool1)
                                            for point in bestPool:
                                                tempPoint = point
                                                tempPooll.remove(tempPoint)

                                            continue

                                        if timess == 1:  # 考虑第二次访问的情况

                                            comNum[i - orderNumberF * 2][t][timess] = nM

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                                # print("k=", k)
                                                couldPos = tempTempBestway[0].index(i)
                                                if k <= couldPos:
                                                    continue
                                                tempSolution = clone(tempTempBestway[0])
                                                # print("ways[t]=", ways[t])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                # print("numberTug=", numberTug)
                                                needPoolAll = list(
                                                    itertools.combinations(tempPooll,
                                                                           nM))  # 从pool1中取出needNumber个数
                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket

                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(couldPos + 1, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t,
                                                                          needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime,
                                                                                        printf=False)
                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=",bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempsolution = clone(ways)
                                            tempsolution[bestJ] = bestWay

                                            comNum[i - orderNumberF * 2][t][0] = nTug1
                                            comNum[i - orderNumberF * 2][t][1] = nM

                                            tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                                            tempTempBestpool.append(bestPool)
                                            if ti == 0:
                                                bestTempTempBestloss = tempTempBestloss
                                                bestTempTempBestj = tempTempBestj
                                                bestTempTempBestway = tempTempBestway
                                                bestTempTempBestpool = tempTempBestpool
                                                ti += 1
                                            else:
                                                if bestTempTempBestloss > tempTempBestloss:
                                                    bestTempTempBestloss = tempTempBestloss
                                                    bestTempTempBestj = tempTempBestj
                                                    bestTempTempBestway = tempTempBestway
                                                    bestTempTempBestpool = tempTempBestpool
                                                ti += 1

                                if bestTempTempBestloss < bTempBestloss:
                                    bTempBestloss = bestTempTempBestloss
                                    bTempBestj = bestTempTempBestj  # [a,b]
                                    bTempBestway = bestTempTempBestway  # [[a,b],[c,d]]
                                    bTempBestpool = bestTempTempBestpool  # [[a,b],[c,d]]

                                tempPool = clone(pool1)
                                for tim in range(len(bTempBestj)):
                                    for point in bTempBestpool[tim]:
                                        tempPoint = point
                                        tempPool.remove(tempPoint)

                                continue

                                # 至此第一个拖船的最优方案已经存在了bTempBestloss、bTempBestj、bTempBestway、bTempBestpool里面
                                # 以及供第二辆拖船选择的驳船都已经存放在tempPool里面了

                            elif t == last:  # maxTugNum=2时才会进入这里
                                # print("t=",t)#对于最后一个元素t来说，他只有一种选择

                                # 不考虑重用
                                comNum[i - orderNumberF * 2][t][0] = numberTug
                                # print("comNum=", comNum)

                                tempBestloss = 0  # 用于临时记录第二辆拖船不重用的最优解
                                tempBestj = []  # 用于临时记录第二辆拖船不重用的最优解
                                tempBestway = []  # 用于临时记录第二辆拖船不重用的最优解
                                tempBestpool = []  # 用于临时记录第二辆拖船不重用的最优解

                                tTempBestloss = 0  # 第二辆拖船的最优解
                                tTempBestj = []  # 第二辆拖船的最优解
                                tTempBestway = []  # 第二辆拖船的最优解
                                tTempBestpool = []  # 第二辆拖船的最优解

                                bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                    # print("k=", k)
                                    if haveAny1(i, ways[t]):
                                        if k != len(ways[t]):
                                            k += 1
                                            continue
                                    if k == len(ways[t]):
                                        pass
                                    else:
                                        if ways[t][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[
                                                ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                    orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    tempSolution = clone(ways[t])
                                    # print("ways[t]=", ways[t])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("numberTug=", numberTug)
                                    needPoolAll = list(
                                        itertools.combinations(tempPool,
                                                               numberTug))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket

                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[tempSolution[
                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[
                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos,
                                                                        tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[t] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = t
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)

                                for timm in range(len(bTempBestj)):  # bTempBestj、bTempBestway这些都是第一辆拖船的部分最优解
                                    tempsolution[bTempBestj[timm]] = bTempBestway[timm]

                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][t][0] = numberTug

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                tempBestj.append(bestJ)
                                tempBestway.append(bestWay)
                                tempBestpool.append(bestPool)

                                tTempBestloss = tempBestloss  # 第二辆拖船的最优解
                                tTempBestj = tempBestj  # 第二辆拖船的最优解
                                tTempBestway = tempBestway  # 第二辆拖船的最优解
                                tTempBestpool = tempBestpool  # 第二辆拖船的最优解

                                bestTempTempBestloss = float("inf")  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                bestTempTempBestj = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                bestTempTempBestway = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                bestTempTempBestpool = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较

                                ti = 0

                                # 考虑重用
                                for needB in range(numberTug):
                                    nB = needB + 1  # 第一辆拖船第一次访问拉的驳船数
                                    nTug2 = numberTug
                                    nTug2 -= nB  # 第一辆拖船第二次访问拉的驳船数
                                    if (nTug2 == 0) or (nTug2 == 1) or (nB == 1):
                                        continue

                                    for timess in range(maxTravelTime):
                                        if timess == 0:  # 考虑第一次访问的情况

                                            tempTempBestloss = 0  # 用于临时记录第二辆拖船重用的最优解
                                            tempTempBestj = []  # 用于临时记录第二辆拖船重用的最优解
                                            tempTempBestway = []  # 用于临时记录第二辆拖船重用的最优解
                                            tempTempBestpool = []  # 用于临时记录第二辆拖船重用的最优解

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            comNum[i - orderNumberF * 2][t][timess] = nTug2

                                            ttempPool = []  # 用于衔接两次分配任务

                                            for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                                # print("k=", k)
                                                if haveAny1(i, ways[t]):
                                                    if k != len(ways[t]):
                                                        k += 1
                                                        continue
                                                if k == len(ways[t]):
                                                    pass
                                                else:
                                                    if ways[t][k] < 2 * orderNumberF:
                                                        if k % 2 == 0:
                                                            if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                            i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[
                                                            ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                            continue
                                                        else:
                                                            pass

                                                tempSolution = clone(ways[t])
                                                # print("ways[j]=", ways[j])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                # print("bNum=", bNum)
                                                needPoolAll = list(
                                                    itertools.combinations(tempPool,
                                                                           nTug2))  # 从pool1中取出needNumber个数
                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket

                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            if haveAny2(i, tempSolution):
                                                                if pos != k:
                                                                    pos += 1
                                                                    continue
                                                            if pos == k:
                                                                pass
                                                            else:
                                                                if tempSolution[pos] < 2 * orderNumberF:
                                                                    if tempSolution[pos] % 2 == 0:
                                                                        if orderF[int((tempSolution[pos]) / 2), 5] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            # print(pos)
                                                                            pass
                                                                    else:
                                                                        if orderF[int((tempSolution[pos]) / 2), 7] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass
                                                                elif tempSolution[
                                                                    pos] < 2 * orderNumberF + orderNumberE1:
                                                                    if orderE1[
                                                                        tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass
                                                                else:
                                                                    if orderE2[tempSolution[
                                                                                   pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                            orderE2[
                                                                                needPool[
                                                                                    0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                        continue
                                                                    else:
                                                                        pass

                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t,
                                                                          needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime, printf=False)

                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=",bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)
                                            tempTempBestpool.append(bestPool)

                                            comNum[i - orderNumberF * 2][t][timess] = nTug2

                                            ttempPool = clone(tempPool)
                                            for point in bestPool:
                                                tempPoint = point
                                                ttempPool.remove(tempPoint)

                                            continue

                                        if timess == 1:  # 考虑第二次访问的情况

                                            comNum[i - orderNumberF * 2][t][timess] = nB

                                            bestJ = 0  # 临时存储一下局部最优解
                                            bestWay = []  # 临时存储一下局部最优解
                                            bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                            bestPool = []  # 临时存储一下局部最优解

                                            for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                                couldPos = tempTempBestway[0].index(i)
                                                if k <= couldPos:
                                                    continue
                                                # print("k=", k)
                                                tempSolution = clone(tempTempBestway[0])
                                                # print("ways[t]=", ways[t])
                                                # print("tempSolution=", tempSolution)
                                                tempSolution.insert(k, i)  # 在此位置插入终点
                                                # print("tempSolution=", tempSolution)
                                                # print("numberTug=", numberTug)
                                                needPoolAll = list(
                                                    itertools.combinations(ttempPool,
                                                                           nB))  # 从pool1中取出needNumber个数
                                                # print("needPoolAll=", needPoolAll)
                                                for p in needPoolAll:
                                                    needPools = list(sorted(p, key=lambda i: orderE2[
                                                        i - 2 * orderNumberF - orderNumberE1, 9]))
                                                    tempBracket = []
                                                    tempBracket.append(needPools)
                                                    needPools = tempBracket

                                                    for needPool in needPools:
                                                        # print("needPool=", needPool)
                                                        for pos in range(couldPos + 1, k + 1):  # pos是pool中第一个元素要插入的位置
                                                            tempNeedPool = clone(needPool)
                                                            tempTempSolution = clone(tempSolution)
                                                            tempK = k
                                                            tempTempSolution.insert(pos, tempNeedPool[
                                                                0])  # 插入pool中的第一个元素
                                                            # print("tempTempSolution=",tempTempSolution)
                                                            tempNeedPool.remove(
                                                                tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                            tempK += 1
                                                            if tempNeedPool != []:
                                                                cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                          pos, t,
                                                                          needPool)
                                                            else:
                                                                temp = []
                                                                for nub in range(courierNumber):
                                                                    temp.append([])
                                                                temp[t] = tempTempSolution
                                                                tempLoss = lossFunction(temp, startTime,
                                                                                        printf=False)
                                                                if (tempLoss < bestLossE2):
                                                                    # print("bestlossE2=",bestLossE2)
                                                                    # print("tempLoss=", tempLoss)

                                                                    bestLossE2 = tempLoss
                                                                    bestJ = t
                                                                    bestWay = clone(tempTempSolution)
                                                                    bestPool = clone(needPool)

                                            tempsolution = clone(ways)
                                            tempsolution[bestJ] = bestWay

                                            comNum[i - orderNumberF * 2][t][0] = nTug2
                                            comNum[i - orderNumberF * 2][t][1] = nB

                                            tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                            tempTempBestj.append(bestJ)
                                            tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                                            tempTempBestpool.append(bestPool)

                                            if ti == 0:
                                                bestTempTempBestloss = tempTempBestloss
                                                bestTempTempBestj = tempTempBestj
                                                bestTempTempBestway = tempTempBestway
                                                bestTempTempBestpool = tempTempBestpool
                                                ti += 1
                                            else:
                                                if bestTempTempBestloss > tempTempBestloss:
                                                    bestTempTempBestloss = tempTempBestloss
                                                    bestTempTempBestj = tempTempBestj
                                                    bestTempTempBestway = tempTempBestway
                                                    bestTempTempBestpool = tempTempBestpool
                                                ti += 1

                                if bestTempTempBestloss < tTempBestloss:
                                    tTempBestloss = bestTempTempBestloss
                                    tTempBestj = bestTempTempBestj  # [a,b]
                                    tTempBestway = bestTempTempBestway  # [[a,b],[c,d]]
                                    tTempBestpool = bestTempTempBestpool  # [[a,b],[c,d]]

                                # 至此，第一辆拖船以及第二辆拖船的最优部分解均已经收集完毕

                                for leng in range(len(tTempBestj)):
                                    bTempBestj.append(tTempBestj[leng])
                                    bTempBestway.append(tTempBestway[leng])
                                    bTempBestpool.append(tTempBestpool[leng])

                                tempsolution = clone(ways)

                                for leng in range(len(bTempBestj)):
                                    tempsolution[bTempBestj[leng]] = bTempBestway[leng]

                                tNum = []

                                for ii in range(orderNumberE1):
                                    tNum.append([])
                                    for jj in range(courierNumber):
                                        tNum[ii].append([])
                                        for _ in range(2):
                                            tNum[ii][jj].append(0)

                                comNum[i - orderNumberF * 2] = tNum[i - orderNumberF * 2]

                                tempp = []
                                for to in bTempBestj:
                                    if to in tempp:
                                        # 查找 to 在 bbestTempBestj 中第二次出现的位置
                                        index = find_nth_occurrence(bTempBestj, to, 2)
                                        if index != -1:
                                            comNum[i - orderNumberF * 2][to][1] = len(bTempBestpool[index])
                                    else:
                                        index = bTempBestj.index(to)
                                        comNum[i - orderNumberF * 2][to][0] = len(bTempBestpool[index])
                                    tempp.append(to)

                                tempBestloss = lossFunction(tempsolution, startTime, printf=False)

                                if tii == 0:
                                    bbestTempBestloss = tempBestloss
                                    bbestTempBestj = bTempBestj
                                    bbestTempBestway = bTempBestway
                                    bbestTempBestpool = bTempBestpool
                                    tii += 1
                                elif tii == 1:
                                    if tempBestloss < bbestTempBestloss:
                                        bbestTempBestlosss = bbestTempBestloss

                                        bbestTempBestloss = tempBestloss
                                        bbestTempBestj = bTempBestj
                                        bbestTempBestway = bTempBestway
                                        bbestTempBestpool = bTempBestpool
                                    else:
                                        bbestTempBestlosss = tempBestloss
                                    tii += 1
                                else:
                                    tii += 1
                                    if (bbestTempBestloss < tempBestloss) and (bbestTempBestlosss > tempBestloss):
                                        bbestTempBestlosss = tempBestloss
                                    elif (tempBestloss < bbestTempBestloss):
                                        bbestTempBestlosss = bbestTempBestloss

                                        bbestTempBestloss = tempBestloss
                                        bbestTempBestj = bTempBestj
                                        bbestTempBestway = bTempBestway
                                        bbestTempBestpool = bTempBestpool
                if (bbestTempBestlosss - bbestTempBestloss) < BestLoss:
                    BestLoss = bbestTempBestlosss - bbestTempBestloss
                    BestJ = bbestTempBestj
                    BestWay = bbestTempBestway
                    BestPool = bbestTempBestpool

        pool1 = tempPool1
        for chosenone in range(len(BestJ)):
            for point in BestPool[chosenone]:
                tempPoint = point
                pool1.remove(tempPoint)
        tempPool1 = pool1

        if timer == 0:
            BestJJ = BestJ
            BestWayy = BestWay
            BestLosss = BestLoss
            BestPooll = BestPool
            BestI = i
            timer += 1
        elif timer != 0:
            if BestLoss < BestLosss:
                BestJJ = BestJ
                BestWayy = BestWay
                BestLosss = BestLoss
                BestPooll = BestPool
                BestI = i

    pool1=tempPool2

    if BestJJ!=[]:
        for chosenone in range(len(BestJJ)):
            ways[BestJJ[chosenone]] = BestWayy[chosenone]
            for point in BestPooll[chosenone]:
                tempPoint = point
                pool1.remove(tempPoint)
        tNum = []

        for ii in range(orderNumberE1):
            tNum.append([])
            for jj in range(courierNumber):
                tNum[ii].append([])
                for _ in range(2):
                    tNum[ii][jj].append(0)

        comNum[BestI - orderNumberF * 2] = tNum[BestI - orderNumberF * 2]

        tempp = []
        for to in BestJJ:
            if to in tempp:
                # 查找 to 在 bbestTempBestj 中第二次出现的位置
                index = find_nth_occurrence(BestJJ, to, 2)
                if index != -1:
                    comNum[BestI - orderNumberF * 2][to][1] = len(BestPooll[index])
            else:
                index = BestJJ.index(to)
                comNum[BestI - orderNumberF * 2][to][0] = len(BestPooll[index])
            tempp.append(to)

        pool2.remove(BestI)

    if pool2 != []:
        for i in pool2:
            # print("i=", i)
            # print("pool1=", pool1)
            # print("pool2=", pool2)
            # print("ways=", ways)
            needNumber = int(orderE1[i - orderNumberF * 2][8])  # 获取E类型订单需要的空驳船数量
            pool1 = pool1[:needNumber]

            tii = 0  # 记录是第几次得到了临时最后解
            bbestTempBestloss = float("inf")  # 临时记录最后解
            bbestTempBestj = []  # 临时记录最后解
            bbestTempBestway = []  # 临时记录最后解
            bbestTempBestpool = []  # 临时记录最后解
            bbestTempBestlosss = float("inf")

            BestJ = []  # 记录单拖船不重用的最优解，记录最后的最优解
            BestWay = []  # 记录单拖船不重用的最优解，记录最后的最优解
            BestLoss = float('inf')  # 记录单拖船不重用的最优解，记录最后的最优解
            BestPool = []  # 记录单拖船不重用的最优解，记录最后的最优解

            bestj = []  # 临时存储一下单拖船不重用整体最优解
            bestway = []  # 临时存储一下单拖船不重用整体最优解
            bestloss = float('inf')  # 临时存储一下单拖船不重用整体最优解
            bestpool = []  # 临时存储一下单拖船不重用整体最优解

            # 单拖船不重用
            if needNumber <= maxcap:
                tugNum = 1
                for j in range(len(ways)):  # 选择拖船
                    bestJ = 0  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                    bestWay = []  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                    bestLossE2 = float('inf')  # 因为要调用递归函数，所以在这里初始化这四个全局变量
                    bestPool = []  # 因为要调用递归函数，所以在这里初始化这四个全局变量

                    comNum[i - orderNumberF * 2][j][0] = needNumber

                    for k in range(len(ways[j]) + 1):  # 选择终点插入位置
                        if haveAny1(i, ways[j]):
                            if k != len(ways[j]):
                                k += 1
                                continue
                        if k == len(ways[j]):
                            pass
                        else:
                            if ways[j][k] < 2 * orderNumberF:
                                if k % 2 == 0:
                                    if orderF[int((ways[j][k]) / 2), 5] < orderE1[i - 2 * orderNumberF, 7]:
                                        continue
                                    else:
                                        pass
                                else:
                                    if orderF[int((ways[j][k]) / 2), 7] < orderE1[i - 2 * orderNumberF, 7]:
                                        continue
                                    else:
                                        pass
                            elif ways[j][k] < 2 * orderNumberF + orderNumberE1:
                                if orderE1[ways[j][k] - 2 * orderNumberF, 7] < orderE1[i - 2 * orderNumberF, 7]:
                                    continue
                                else:
                                    pass
                            else:
                                if orderE2[ways[j][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                    i - 2 * orderNumberF, 7]:
                                    continue
                                else:
                                    pass
                        tempSolution = clone(ways[j])
                        # print("ways[j]=", ways[j])
                        # print("tempSolution=", tempSolution)
                        tempSolution.insert(k, i)  # 在此位置插入终点
                        # print("tempSolution=", tempSolution)
                        needPoolAll = list(itertools.combinations(pool1, needNumber))  # 从pool1中取出needNumber个数
                        for p in needPoolAll:
                            # print("p=", p)
                            needPools = list(sorted(p, key=lambda i: orderE2[
                                i - 2 * orderNumberF - orderNumberE1, 9]))
                            tempBracket = []
                            tempBracket.append(needPools)
                            needPools = tempBracket

                            for needPool in needPools:
                                for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                    if haveAny2(i, tempSolution):
                                        if pos != k:
                                            pos += 1
                                            continue
                                    if pos == k:
                                        pass
                                    else:
                                        if tempSolution[pos] < 2 * orderNumberF:
                                            if tempSolution[pos] % 2 == 0:
                                                if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                    continue
                                                else:
                                                    # print(pos)
                                                    pass
                                            else:
                                                if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                    continue
                                                else:
                                                    pass
                                        elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < orderE2[
                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[tempSolution[pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                    orderE2[
                                                        needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                continue
                                            else:
                                                pass
                                    tempNeedPool = clone(needPool)
                                    tempTempSolution = clone(tempSolution)
                                    tempK = k
                                    tempTempSolution.insert(pos, tempNeedPool[0])  # 插入pool中的第一个元素
                                    # print("tempTempSolution=",tempTempSolution)
                                    tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                    tempK += 1
                                    if tempNeedPool != []:
                                        cirInsert(tempK, tempTempSolution, tempNeedPool, pos, j, needPool)
                                    else:
                                        temp = []
                                        for nub in range(courierNumber):
                                            temp.append([])
                                        temp[j] = tempTempSolution
                                        tempLoss = lossFunction(temp, startTime, printf=False)
                                        if (tempLoss < bestLossE2):
                                            # print("bestlossE2=",bestLossE2)
                                            # print("tempLoss=", tempLoss)

                                            bestLossE2 = tempLoss
                                            bestJ = j
                                            bestWay = clone(tempTempSolution)
                                            bestPool = clone(needPool)
                                            # print("bestLossE2=", bestLossE2)
                                            # print("bestPool=", bestPool)
                                            # print("bestWay=", bestWay)
                                            # print("bestJ=", bestJ)

                    tempsolution = clone(ways)
                    tempsolution[bestJ] = bestWay
                    # print("bestWay=",bestWay)
                    if (j == 0):
                        # print("j=",j)
                        bestloss = lossFunction(tempsolution, startTime, printf=False)
                        # print("bestloss=", bestloss)
                        bestj = bestJ
                        bestway = bestWay
                        bestpool = bestPool
                    elif j == 1:
                        tempLoss = lossFunction(tempsolution, startTime, printf=False)
                        if tempLoss < bestloss:
                            bestlosss = bestloss

                            bestloss = tempLoss
                            bestj = bestJ
                            bestway = bestWay
                            bestpool = bestPool
                    else:
                        # print("j=", j)
                        tempLoss = lossFunction(tempsolution, startTime, printf=False)
                        # print("tempLoss=", tempLoss)
                        if (tempLoss < bestlosss) and (tempLoss > bestloss):
                            bestlosss = tempLoss
                        elif (tempLoss < bestloss):
                            bestlosss = bestloss

                            bestloss = tempLoss
                            bestj = bestJ
                            bestway = bestWay
                            bestpool = bestPool

                BestLoss = bestlosss - bestloss  # 记录单拖船不重用的最优解，记录最后的最优解
                BestJ.append(bestj)  # 记录单拖船不重用的最优解，记录最后的最优解
                BestWay.append(bestway)  # 记录单拖船不重用的最优解，记录最后的最优解
                BestPool.append(bestpool)  # 记录单拖船不重用的最优解，记录最后的最优解

            # 单拖船重用+两拖船不重用
            elif needNumber <= 2 * maxcap:
                # 单拖船重用
                tugNum = 1
                bestTempBestloss = float("inf")  # 记录单拖船重用的最优解
                bestTempBestj = []  # 记录单拖船重用的最优解
                bestTempBestway = []  # 记录单拖船重用的最优解
                bestTempBestpool = []  # 记录单拖船重用的最优解
                bestTempBestlosss = float("inf")

                ti = 0  # 记录是第几次得到tempBestj、tempBestway、tempBestloss、tempBestpool了

                for j in range(len(ways)):  # 选择拖船
                    # print("j=",j)
                    for needN in range(needNumber):
                        numberTug = needNumber  # numberTug是一个全局变量
                        nN = needN + 1  # bNum是第一次访问拉空驳船的数量
                        # print("bNum=",bNum)
                        numberTug -= nN  # numberTug是第二次访问拉空驳船的数量
                        if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (nN == 1) or (
                                nN == 2):  # numberTug=0意味着第二次访问拉的数量为0，相当于只考虑了访问一次
                            continue

                        for times in range(maxTravelTime):
                            if times == 0:  # 考虑第一次访问的情况

                                tempTempBestloss = 0  # 临时存储第一次访问与第二次访问的解
                                tempTempBestj = []  # 临时存储第一次访问与第二次访问的解
                                tempTempBestway = []  # 临时存储第一次访问与第二次访问的解
                                tempTempBestpool = []  # 临时存储第一次访问与第二次访问的解

                                comNum[i - orderNumberF * 2][j][times] = numberTug

                                bestJ = 0  # 临时存储一下局部最优解
                                bestWay = []  # 临时存储一下局部最优解
                                bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                bestPool = []  # 临时存储一下局部最优解

                                tempPool = []  # 用于衔接两次分配任务

                                for k in range(len(ways[j]) + 1):  # 选择终点插入位置
                                    if haveAny1(i, ways[j]):
                                        if k != len(ways[j]):
                                            k += 1
                                            continue
                                    if k == len(ways[j]):
                                        pass
                                    else:
                                        if ways[j][k] < 2 * orderNumberF:
                                            if k % 2 == 0:
                                                if orderF[int((ways[j][k]) / 2), 5] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderF[int((ways[j][k]) / 2), 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                        elif ways[j][k] < 2 * orderNumberF + orderNumberE1:
                                            if orderE1[ways[j][k] - 2 * orderNumberF, 7] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass
                                        else:
                                            if orderE2[ways[j][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                                i - 2 * orderNumberF, 7]:
                                                continue
                                            else:
                                                pass

                                    tempSolution = clone(ways[j])
                                    # print("ways[j]=", ways[j])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("bNum=", bNum)
                                    needPoolAll = list(
                                        itertools.combinations(pool1, numberTug))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket
                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                if haveAny2(i, tempSolution):
                                                    if pos != k:
                                                        pos += 1
                                                        continue
                                                if pos == k:
                                                    pass
                                                else:
                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                        if tempSolution[pos] % 2 == 0:
                                                            if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                # print(pos)
                                                                pass
                                                        else:
                                                            if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                    elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                        if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < orderE2[
                                                            needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass
                                                    else:
                                                        if orderE2[
                                                            tempSolution[pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                            continue
                                                        else:
                                                            pass

                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos, tempNeedPool[
                                                    0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(
                                                    tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                              pos, j,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[j] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime,
                                                                            printf=False)

                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = j
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempTempBestj.append(bestJ)
                                tempTempBestway.append(bestWay)
                                tempTempBestpool.append(bestPool)

                                tempPool = clone(pool1)
                                for point in bestPool:
                                    tempPoint = point
                                    tempPool.remove(tempPoint)

                                continue

                            if times == 1:  # 考虑第二次访问的情况

                                comNum[i - orderNumberF * 2][j][times] = nN

                                bestJ = 0  # 临时存储一下局部最优解
                                bestWay = []  # 临时存储一下局部最优解
                                bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                bestPool = []  # 临时存储一下局部最优解

                                for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                    # print("k=", k)
                                    couldPos = tempTempBestway[0].index(i)
                                    if k <= couldPos:
                                        continue
                                    tempSolution = clone(tempTempBestway[0])
                                    # print("ways[t]=", ways[t])
                                    # print("tempSolution=", tempSolution)
                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                    # print("tempSolution=", tempSolution)
                                    # print("numberTug=", numberTug)
                                    needPoolAll = list(itertools.combinations(tempPool, nN))  # 从pool1中取出needNumber个数
                                    # print("needPoolAll=", needPoolAll)
                                    for p in needPoolAll:
                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                        tempBracket = []
                                        tempBracket.append(needPools)
                                        needPools = tempBracket

                                        for needPool in needPools:
                                            # print("needPool=", needPool)
                                            for pos in range(couldPos + 1, k + 1):  # pos是pool中第一个元素要插入的位置
                                                tempNeedPool = clone(needPool)
                                                tempTempSolution = clone(tempSolution)
                                                tempK = k
                                                tempTempSolution.insert(pos, tempNeedPool[0])  # 插入pool中的第一个元素
                                                # print("tempTempSolution=",tempTempSolution)
                                                tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                tempK += 1
                                                if tempNeedPool != []:
                                                    cirInsert(tempK, tempTempSolution, tempNeedPool, pos, j,
                                                              needPool)
                                                else:
                                                    temp = []
                                                    for nub in range(courierNumber):
                                                        temp.append([])
                                                    temp[j] = tempTempSolution
                                                    tempLoss = lossFunction(temp, startTime, printf=False)
                                                    if (tempLoss < bestLossE2):
                                                        # print("bestlossE2=",bestLossE2)
                                                        # print("tempLoss=", tempLoss)

                                                        bestLossE2 = tempLoss
                                                        bestJ = j
                                                        bestWay = clone(tempTempSolution)
                                                        bestPool = clone(needPool)

                                tempsolution = clone(ways)
                                tempsolution[bestJ] = bestWay

                                comNum[i - orderNumberF * 2][j][0] = numberTug
                                comNum[i - orderNumberF * 2][j][1] = nN

                                tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                tempTempBestj.append(bestJ)
                                tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                                tempTempBestpool.append(bestPool)

                                if ti == 0:
                                    bestTempBestloss = tempTempBestloss
                                    bestTempBestj = tempTempBestj
                                    bestTempBestway = tempTempBestway
                                    bestTempBestpool = tempTempBestpool
                                    ti += 1
                                elif ti == 1:
                                    if tempTempBestloss < bestTempBestloss:
                                        bestTempBestlosss = bestTempBestloss

                                        bestTempBestloss = tempTempBestloss
                                        bestTempBestj = tempTempBestj
                                        bestTempBestway = tempTempBestway
                                        bestTempBestpool = tempTempBestpool
                                    else:
                                        bestTempBestlosss = tempTempBestloss
                                    ti += 1
                                else:
                                    if (bestTempBestlosss > tempTempBestloss) and (tempTempBestloss > bestTempBestloss):
                                        bestTempBestlosss = tempTempBestloss
                                    elif (tempTempBestloss < bestTempBestloss):
                                        bestTempBestlosss = bestTempBestloss

                                        bestTempBestloss = tempTempBestloss
                                        bestTempBestj = tempTempBestj
                                        bestTempBestway = tempTempBestway
                                        bestTempBestpool = tempTempBestpool
                                    ti += 1

                if bestTempBestloss < BestLoss:
                    BestLoss = bestTempBestloss - bestTempBestlosss
                    BestJ = bestTempBestj
                    BestWay = bestTempBestway
                    BestPool = bestTempBestpool

                # 两拖船不重用
                bTempBestloss = float("inf")  # 两拖船不重用的最优解
                bTempBestj = []  # 两拖船不重用的最优解
                bTempBestway = []  # 两拖船不重用的最优解
                bTempBestpool = []  # 两拖船不重用的最优解
                bTempBestlosss = float("inf")

                tugNum = 2
                ti = 0
                tugAll = list(itertools.combinations(tugs, tugNum))
                # print("tugAll=",tugAll)
                for n in tugAll:
                    tug = list(itertools.permutations(n))
                    # print("tug=",tug)
                    for tu in tug:
                        # print("tu=",tu)
                        # print("needNumber=",needNumber)
                        for bargeNum in range(needNumber):
                            # print("bargeNum=",bargeNum)
                            numberTug = needNumber  # numberTug是一个全局变量
                            bNum = bargeNum + 1  # bNum是第一艘拖船拉空驳船的数量
                            # print("bNum=",bNum)
                            numberTug -= bNum  # numberTug是现在还有多少空驳船需要拉
                            # print("numberTug=",numberTug)

                            if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (bNum == 1) or (bNum == 2):
                                # print("break")
                                continue

                            for t in tu:
                                first = tu[0]
                                last = tu[-1]
                                if t == first:
                                    comNum[i - orderNumberF * 2][t][0] = bNum

                                    bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                    tempPool = []  # 用于用于衔接两辆拖船

                                    tempBestj = []  # 用于临时记录拖船的单拖船最优方案
                                    tempBestway = []  # 用于临时记录拖船的单拖船最优方案
                                    tempBestloss = 0  # 用于临时记录拖船的单拖船最优方案
                                    tempBestpool = []  # 用于临时记录拖船的单拖船最优方案

                                    # 不考虑重用
                                    for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                        if haveAny1(i, ways[t]):
                                            if k != len(ways[t]):
                                                k += 1
                                                continue
                                        if k == len(ways[t]):
                                            pass
                                        else:
                                            if ways[t][k] < 2 * orderNumberF:
                                                if k % 2 == 0:
                                                    if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                                else:
                                                    if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                            elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderE2[ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass

                                        # print("k=", k)
                                        tempSolution = clone(ways[t])
                                        # print("ways[j]=", ways[j])
                                        # print("tempSolution=", tempSolution)
                                        tempSolution.insert(k, i)  # 在此位置插入终点
                                        # print("tempSolution=", tempSolution)
                                        # print("bNum=", bNum)
                                        needPoolAll = list(
                                            itertools.combinations(pool1, bNum))  # 从pool1中取出needNumber个数
                                        # print("needPoolAll=", needPoolAll)
                                        for p in needPoolAll:
                                            needPools = list(sorted(p, key=lambda i: orderE2[
                                                i - 2 * orderNumberF - orderNumberE1, 9]))
                                            tempBracket = []
                                            tempBracket.append(needPools)
                                            needPools = tempBracket
                                            # if bNum==8:
                                            #     print(needPools)
                                            # if needPools==[[31,32,33,34,35,36,37,38]]:
                                            #     print("right here!")
                                            for needPool in needPools:
                                                # print("needPool=", needPool)
                                                for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                    if haveAny2(i, tempSolution):
                                                        if pos != k:
                                                            pos += 1
                                                            continue
                                                    if pos == k:
                                                        pass
                                                    else:
                                                        if tempSolution[pos] < 2 * orderNumberF:
                                                            if tempSolution[pos] % 2 == 0:
                                                                if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    # print(pos)
                                                                    pass
                                                            else:
                                                                if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    pass
                                                        elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                            if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderE2[tempSolution[
                                                                           pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass

                                                    tempNeedPool = clone(needPool)
                                                    tempTempSolution = clone(tempSolution)
                                                    tempK = k
                                                    tempTempSolution.insert(pos,
                                                                            tempNeedPool[0])  # 插入pool中的第一个元素
                                                    # print("tempTempSolution=",tempTempSolution)
                                                    tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                    tempK += 1
                                                    if tempNeedPool != []:
                                                        cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                                  needPool)
                                                    else:
                                                        temp = []
                                                        for nub in range(courierNumber):
                                                            temp.append([])
                                                        temp[t] = tempTempSolution
                                                        tempLoss = lossFunction(temp, startTime, printf=False)
                                                        if (tempLoss < bestLossE2):
                                                            # print("bestlossE2=",bestLossE2)
                                                            # print("tempLoss=", tempLoss)

                                                            bestLossE2 = tempLoss
                                                            bestJ = t
                                                            bestWay = clone(tempTempSolution)
                                                            bestPool = clone(needPool)

                                    tempsolution = clone(ways)
                                    tempsolution[bestJ] = bestWay

                                    comNum[i - orderNumberF * 2][t][0] = bNum

                                    tempBestj.append(bestJ)  # t拖船的单拖船最优方案
                                    tempBestway.append(bestWay)  # t拖船的单拖船最优方案
                                    tempBestpool.append(bestPool)  # t拖船的单拖船最优方案

                                    tempPool = clone(pool1)
                                    for tim in range(len(tempBestj)):
                                        for point in tempBestpool[tim]:
                                            tempPoint = point
                                            tempPool.remove(tempPoint)

                                    continue
                                elif t == last:
                                    comNum[i - orderNumberF * 2][t][0] = numberTug
                                    # print("comNum=", comNum)

                                    bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                    for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                        # print("k=", k)
                                        if haveAny1(i, ways[t]):
                                            if k != len(ways[t]):
                                                k += 1
                                                continue
                                        if k == len(ways[t]):
                                            pass
                                        else:
                                            if ways[t][k] < 2 * orderNumberF:
                                                if k % 2 == 0:
                                                    if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                                else:
                                                    if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                            elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderE2[
                                                    ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                        orderE1[
                                                            i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass

                                        tempSolution = clone(ways[t])
                                        # print("ways[t]=", ways[t])
                                        # print("tempSolution=", tempSolution)
                                        tempSolution.insert(k, i)  # 在此位置插入终点
                                        # print("tempSolution=", tempSolution)
                                        # print("numberTug=", numberTug)
                                        needPoolAll = list(
                                            itertools.combinations(tempPool,
                                                                   numberTug))  # 从pool1中取出needNumber个数
                                        # print("needPoolAll=", needPoolAll)
                                        for p in needPoolAll:
                                            needPools = list(sorted(p, key=lambda i: orderE2[
                                                i - 2 * orderNumberF - orderNumberE1, 9]))
                                            tempBracket = []
                                            tempBracket.append(needPools)
                                            needPools = tempBracket

                                            for needPool in needPools:
                                                # print("needPool=", needPool)
                                                for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                    if haveAny2(i, tempSolution):
                                                        if pos != k:
                                                            pos += 1
                                                            continue
                                                    if pos == k:
                                                        pass
                                                    else:
                                                        if tempSolution[pos] < 2 * orderNumberF:
                                                            if tempSolution[pos] % 2 == 0:
                                                                if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    # print(pos)
                                                                    pass
                                                            else:
                                                                if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    pass
                                                        elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                            if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderE2[tempSolution[
                                                                           pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass

                                                    tempNeedPool = clone(needPool)
                                                    tempTempSolution = clone(tempSolution)
                                                    tempK = k
                                                    tempTempSolution.insert(pos,
                                                                            tempNeedPool[0])  # 插入pool中的第一个元素
                                                    # print("tempTempSolution=",tempTempSolution)
                                                    tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                    tempK += 1
                                                    if tempNeedPool != []:
                                                        cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                                  needPool)
                                                    else:
                                                        temp = []
                                                        for nub in range(courierNumber):
                                                            temp.append([])
                                                        temp[t] = tempTempSolution
                                                        tempLoss = lossFunction(temp, startTime, printf=False)
                                                        if (tempLoss < bestLossE2):
                                                            # print("bestlossE2=",bestLossE2)
                                                            # print("tempLoss=", tempLoss)

                                                            bestLossE2 = tempLoss
                                                            bestJ = t
                                                            bestWay = clone(tempTempSolution)
                                                            bestPool = clone(needPool)

                                    tempsolution = clone(ways)

                                    for timm in range(len(tempBestj)):
                                        tempsolution[tempBestj[timm]] = tempBestway[timm]

                                    tempsolution[bestJ] = bestWay

                                    comNum[i - orderNumberF * 2][t][0] = numberTug

                                    tempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                    tempBestj.append(bestJ)
                                    tempBestway.append(bestWay)
                                    tempBestpool.append(bestPool)

                                    if ti == 0:
                                        bTempBestloss = tempBestloss  # 两拖船不重用的最优解
                                        bTempBestj = tempBestj  # 两拖船不重用的最优解
                                        bTempBestway = tempBestway  # 两拖船不重用的最优解
                                        bTempBestpool = tempBestpool  # 两拖船不重用的最优解
                                        ti += 1
                                    elif ti == 1:
                                        if tempBestloss < bTempBestloss:
                                            bTempBestlosss = bTempBestloss

                                            bTempBestloss = tempBestloss  # 两拖船不重用的最优解
                                            bTempBestj = tempBestj  # 两拖船不重用的最优解
                                            bTempBestway = tempBestway  # 两拖船不重用的最优解
                                            bTempBestpool = tempBestpool  # 两拖船不重用的最优解
                                        else:
                                            bTempBestlosss = tempBestloss
                                        ti += 1
                                    else:
                                        if (tempBestloss < bTempBestlosss) and (tempBestloss > bTempBestloss):
                                            bTempBestlosss = tempBestloss  # 两拖船不重用的最优解
                                        elif (tempBestloss < bTempBestloss):
                                            bTempBestlosss = bTempBestloss

                                            bTempBestloss = tempBestloss
                                            bTempBestj = tempBestj  # 两拖船不重用的最优解
                                            bTempBestway = tempBestway  # 两拖船不重用的最优解
                                            bTempBestpool = tempBestpool  # 两拖船不重用的最优解
                                        ti += 1
                if (bTempBestlosss - bTempBestloss) < BestLoss:
                    BestLoss = bTempBestlosss - bTempBestloss
                    BestJ = bTempBestj
                    BestWay = bTempBestway
                    BestPool = bTempBestpool

            # 双拖船重用
            elif needNumber <= 3 * maxcap:
                tugNum = 2

                numberTug = 0  # 用于记录第二个拖船需要拉多少个驳船
                nTug1 = 0
                nTug2 = 0

                tugAll = list(itertools.combinations(tugs, tugNum))
                # print("tugAll=",tugAll)
                for n in tugAll:
                    tug = list(itertools.permutations(n))
                    # print("tug=",tug)
                    for tu in tug:
                        # print("tu=",tu)
                        # print("needNumber=",needNumber)
                        for bargeNum in range(needNumber):
                            # print("bargeNum=",bargeNum)
                            numberTug = needNumber  # numberTug是一个全局变量
                            bNum = bargeNum + 1  # bNum是第一艘拖船拉空驳船的数量
                            # print("bNum=",bNum)
                            numberTug -= bNum  # numberTug是现在还有多少空驳船需要拉
                            # print("numberTug=",numberTug)

                            if (numberTug == 0) or (numberTug == 1) or (numberTug == 2) or (bNum == 1) or (bNum == 2):
                                # print("break")
                                continue

                            for t in tu:
                                first = tu[0]
                                last = tu[-1]
                                if t == first:

                                    bTempBestloss = 0  # 第一辆拖船的部分最优解
                                    bTempBestj = []  # 第一辆拖船的部分最优解
                                    bTempBestway = []  # 第一辆拖船的部分最优解
                                    bTempBestpool = []  # 第一辆拖船的部分最优解

                                    comNum[i - orderNumberF * 2][t][0] = bNum
                                    # print("comNum=", comNum)
                                    # print("t=",t)

                                    bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                    tempPool = []  # 用于用于衔接两辆拖船

                                    tempBestj = []  # 用于临时记录拖船的单拖船最优方案
                                    tempBestway = []  # 用于临时记录拖船的单拖船最优方案
                                    tempBestloss = 0  # 用于临时记录拖船的单拖船最优方案
                                    tempBestpool = []  # 用于临时记录拖船的单拖船最优方案

                                    # 不考虑重用
                                    for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                        if haveAny1(i, ways[t]):
                                            if k != len(ways[t]):
                                                k += 1
                                                continue
                                        if k == len(ways[t]):
                                            pass
                                        else:
                                            if ways[t][k] < 2 * orderNumberF:
                                                if k % 2 == 0:
                                                    if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                                else:
                                                    if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                            elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderE2[ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass

                                        # print("k=", k)
                                        tempSolution = clone(ways[t])
                                        # print("ways[j]=", ways[j])
                                        # print("tempSolution=", tempSolution)
                                        tempSolution.insert(k, i)  # 在此位置插入终点
                                        # print("tempSolution=", tempSolution)
                                        # print("bNum=", bNum)
                                        needPoolAll = list(
                                            itertools.combinations(pool1, bNum))  # 从pool1中取出needNumber个数
                                        # print("needPoolAll=", needPoolAll)
                                        for p in needPoolAll:
                                            needPools = list(sorted(p, key=lambda i: orderE2[
                                                i - 2 * orderNumberF - orderNumberE1, 9]))
                                            tempBracket = []
                                            tempBracket.append(needPools)
                                            needPools = tempBracket
                                            # if bNum==8:
                                            #     print(needPools)
                                            # if needPools==[[31,32,33,34,35,36,37,38]]:
                                            #     print("right here!")
                                            for needPool in needPools:
                                                # print("needPool=", needPool)
                                                for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                    if haveAny2(i, tempSolution):
                                                        if pos != k:
                                                            pos += 1
                                                            continue
                                                    if pos == k:
                                                        pass
                                                    else:
                                                        if tempSolution[pos] < 2 * orderNumberF:
                                                            if tempSolution[pos] % 2 == 0:
                                                                if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    # print(pos)
                                                                    pass
                                                            else:
                                                                if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    pass
                                                        elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                            if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderE2[tempSolution[
                                                                           pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass

                                                    tempNeedPool = clone(needPool)
                                                    tempTempSolution = clone(tempSolution)
                                                    tempK = k
                                                    tempTempSolution.insert(pos,
                                                                            tempNeedPool[0])  # 插入pool中的第一个元素
                                                    # print("tempTempSolution=",tempTempSolution)
                                                    tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                    tempK += 1
                                                    if tempNeedPool != []:
                                                        cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                                  needPool)
                                                    else:
                                                        temp = []
                                                        for nub in range(courierNumber):
                                                            temp.append([])
                                                        temp[t] = tempTempSolution
                                                        tempLoss = lossFunction(temp, startTime, printf=False)
                                                        if (tempLoss < bestLossE2):
                                                            # print("bestlossE2=",bestLossE2)
                                                            # print("tempLoss=", tempLoss)

                                                            bestLossE2 = tempLoss
                                                            bestJ = t
                                                            bestWay = clone(tempTempSolution)
                                                            bestPool = clone(needPool)

                                    tempsolution = clone(ways)
                                    tempsolution[bestJ] = bestWay

                                    comNum[i - orderNumberF * 2][t][0] = bNum

                                    tempBestloss = lossFunction(tempsolution, startTime, printf=False)  # t拖船的单拖船最优方案
                                    tempBestj.append(bestJ)  # t拖船的单拖船最优方案
                                    tempBestway.append(bestWay)  # t拖船的单拖船最优方案
                                    tempBestpool.append(bestPool)  # t拖船的单拖船最优方案

                                    bTempBestloss = tempBestloss  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                    bTempBestj = tempBestj  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                    bTempBestway = tempBestway  # 把t拖船的第一次得到的单拖船最优方案存在这里面
                                    bTempBestpool = tempBestpool  # 把t拖船的第一次得到的单拖船最优方案存在这里面

                                    bestTempTempBestloss = float('inf')  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                    bestTempTempBestj = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                    bestTempTempBestway = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较
                                    bestTempTempBestpool = []  # 记录第一辆拖船的重用最优解，一会儿与bTempBestloss比较

                                    ti = 0  # 用于记录是第几次得到了tempTempBestloss

                                    # 考虑重用
                                    for needM in range(bNum):
                                        nM = needM + 1  # 第一辆拖船第一次访问拉的驳船数
                                        nTug1 = bNum
                                        nTug1 -= nM  # 第一辆拖船第二次访问拉的驳船数
                                        if (nTug1 == 0) or (nTug1 == 1) or (nM == 1):
                                            continue
                                        for timess in range(maxTravelTime):

                                            if timess == 0:  # 考虑第一次访问的情况

                                                tempTempBestloss = 0  # 临时记录第一辆拖船的重用最优解
                                                tempTempBestj = []  # 临时记录第一辆拖船的重用最优解
                                                tempTempBestway = []  # 临时记录第一辆拖船的重用最优解
                                                tempTempBestpool = []  # 临时记录第一辆拖船的重用最优解

                                                bestJ = 0  # 临时存储一下局部最优解
                                                bestWay = []  # 临时存储一下局部最优解
                                                bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                                bestPool = []  # 临时存储一下局部最优解

                                                comNum[i - orderNumberF * 2][t][timess] = nTug1

                                                tempPooll = []  # 用于衔接两次访问

                                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                                    # print("k=", k)
                                                    if haveAny1(i, ways[t]):
                                                        if k != len(ways[t]):
                                                            k += 1
                                                            continue
                                                    if k == len(ways[t]):
                                                        pass
                                                    else:
                                                        if ways[t][k] < 2 * orderNumberF:
                                                            if k % 2 == 0:
                                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                                    continue
                                                                else:
                                                                    pass
                                                            else:
                                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                                    continue
                                                                else:
                                                                    pass
                                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderE2[
                                                                ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                    orderE1[
                                                                        i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass

                                                    tempSolution = clone(ways[t])
                                                    # print("ways[j]=", ways[t])
                                                    # print("tempSolution=", tempSolution)
                                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                                    # print("tempSolution=", tempSolution)
                                                    needPoolAll = list(
                                                        itertools.combinations(pool1, nTug1))  # 从pool1中取出needNumber个数

                                                    # print("needPoolAll=", needPoolAll)
                                                    for p in needPoolAll:
                                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                                        tempBracket = []
                                                        tempBracket.append(needPools)
                                                        needPools = tempBracket
                                                        for needPool in needPools:
                                                            # print("needPool=", needPool)
                                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                                if haveAny2(i, tempSolution):
                                                                    if pos != k:
                                                                        pos += 1
                                                                        continue
                                                                if pos == k:
                                                                    pass
                                                                else:
                                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                                        if tempSolution[pos] % 2 == 0:
                                                                            if orderF[int((tempSolution[pos]) / 2), 5] < \
                                                                                    orderE2[
                                                                                        needPool[
                                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                                continue
                                                                            else:
                                                                                # print(pos)
                                                                                pass
                                                                        else:
                                                                            if orderF[int((tempSolution[pos]) / 2), 7] < \
                                                                                    orderE2[
                                                                                        needPool[
                                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                                continue
                                                                            else:
                                                                                pass
                                                                    elif tempSolution[
                                                                        pos] < 2 * orderNumberF + orderNumberE1:
                                                                        if orderE1[
                                                                            tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass
                                                                    else:
                                                                        if orderE2[tempSolution[
                                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass

                                                                tempNeedPool = clone(needPool)
                                                                tempTempSolution = clone(tempSolution)
                                                                tempK = k
                                                                tempTempSolution.insert(pos, tempNeedPool[
                                                                    0])  # 插入pool中的第一个元素
                                                                # print("tempTempSolution=",tempTempSolution)
                                                                tempNeedPool.remove(
                                                                    tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                                tempK += 1
                                                                if tempNeedPool != []:
                                                                    cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                              pos, t, needPool)
                                                                else:
                                                                    temp = []
                                                                    for nub in range(courierNumber):
                                                                        temp.append([])
                                                                    temp[t] = tempTempSolution
                                                                    tempLoss = lossFunction(temp, startTime,
                                                                                            printf=False)
                                                                    # print("tempLoss=", tempLoss)

                                                                    if (tempLoss < bestLossE2):
                                                                        # print("bestlossE2=", bestLossE2)
                                                                        # print("tempLoss=", tempLoss)

                                                                        bestLossE2 = tempLoss
                                                                        bestJ = t
                                                                        bestWay = clone(tempTempSolution)
                                                                        bestPool = clone(needPool)

                                                tempTempBestj.append(bestJ)
                                                tempTempBestway.append(bestWay)
                                                tempTempBestpool.append(bestPool)

                                                tempPooll = clone(pool1)
                                                for point in bestPool:
                                                    tempPoint = point
                                                    tempPooll.remove(tempPoint)

                                                continue

                                            if timess == 1:  # 考虑第二次访问的情况

                                                comNum[i - orderNumberF * 2][t][timess] = nM

                                                bestJ = 0  # 临时存储一下局部最优解
                                                bestWay = []  # 临时存储一下局部最优解
                                                bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                                bestPool = []  # 临时存储一下局部最优解

                                                for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                                    # print("k=", k)
                                                    couldPos = tempTempBestway[0].index(i)
                                                    if k <= couldPos:
                                                        continue
                                                    tempSolution = clone(tempTempBestway[0])
                                                    # print("ways[t]=", ways[t])
                                                    # print("tempSolution=", tempSolution)
                                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                                    # print("tempSolution=", tempSolution)
                                                    # print("numberTug=", numberTug)
                                                    needPoolAll = list(
                                                        itertools.combinations(tempPooll,
                                                                               nM))  # 从pool1中取出needNumber个数
                                                    # print("needPoolAll=", needPoolAll)
                                                    for p in needPoolAll:
                                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                                        tempBracket = []
                                                        tempBracket.append(needPools)
                                                        needPools = tempBracket

                                                        for needPool in needPools:
                                                            # print("needPool=", needPool)
                                                            for pos in range(couldPos + 1,
                                                                             k + 1):  # pos是pool中第一个元素要插入的位置
                                                                tempNeedPool = clone(needPool)
                                                                tempTempSolution = clone(tempSolution)
                                                                tempK = k
                                                                tempTempSolution.insert(pos, tempNeedPool[
                                                                    0])  # 插入pool中的第一个元素
                                                                # print("tempTempSolution=",tempTempSolution)
                                                                tempNeedPool.remove(
                                                                    tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                                tempK += 1
                                                                if tempNeedPool != []:
                                                                    cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                              pos, t,
                                                                              needPool)
                                                                else:
                                                                    temp = []
                                                                    for nub in range(courierNumber):
                                                                        temp.append([])
                                                                    temp[t] = tempTempSolution
                                                                    tempLoss = lossFunction(temp, startTime,
                                                                                            printf=False)
                                                                    if (tempLoss < bestLossE2):
                                                                        # print("bestlossE2=",bestLossE2)
                                                                        # print("tempLoss=", tempLoss)

                                                                        bestLossE2 = tempLoss
                                                                        bestJ = t
                                                                        bestWay = clone(tempTempSolution)
                                                                        bestPool = clone(needPool)

                                                tempsolution = clone(ways)
                                                tempsolution[bestJ] = bestWay

                                                comNum[i - orderNumberF * 2][t][0] = nTug1
                                                comNum[i - orderNumberF * 2][t][1] = nM

                                                tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                                tempTempBestj.append(bestJ)
                                                tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                                                tempTempBestpool.append(bestPool)
                                                if ti == 0:
                                                    bestTempTempBestloss = tempTempBestloss
                                                    bestTempTempBestj = tempTempBestj
                                                    bestTempTempBestway = tempTempBestway
                                                    bestTempTempBestpool = tempTempBestpool
                                                    ti += 1
                                                else:
                                                    if bestTempTempBestloss > tempTempBestloss:
                                                        bestTempTempBestloss = tempTempBestloss
                                                        bestTempTempBestj = tempTempBestj
                                                        bestTempTempBestway = tempTempBestway
                                                        bestTempTempBestpool = tempTempBestpool
                                                    ti += 1

                                    if bestTempTempBestloss < bTempBestloss:
                                        bTempBestloss = bestTempTempBestloss
                                        bTempBestj = bestTempTempBestj  # [a,b]
                                        bTempBestway = bestTempTempBestway  # [[a,b],[c,d]]
                                        bTempBestpool = bestTempTempBestpool  # [[a,b],[c,d]]

                                    tempPool = clone(pool1)
                                    for tim in range(len(bTempBestj)):
                                        for point in bTempBestpool[tim]:
                                            tempPoint = point
                                            tempPool.remove(tempPoint)

                                    continue

                                    # 至此第一个拖船的最优方案已经存在了bTempBestloss、bTempBestj、bTempBestway、bTempBestpool里面
                                    # 以及供第二辆拖船选择的驳船都已经存放在tempPool里面了

                                elif t == last:  # maxTugNum=2时才会进入这里
                                    # print("t=",t)#对于最后一个元素t来说，他只有一种选择

                                    # 不考虑重用
                                    comNum[i - orderNumberF * 2][t][0] = numberTug
                                    # print("comNum=", comNum)

                                    tempBestloss = 0  # 用于临时记录第二辆拖船不重用的最优解
                                    tempBestj = []  # 用于临时记录第二辆拖船不重用的最优解
                                    tempBestway = []  # 用于临时记录第二辆拖船不重用的最优解
                                    tempBestpool = []  # 用于临时记录第二辆拖船不重用的最优解

                                    tTempBestloss = 0  # 第二辆拖船的最优解
                                    tTempBestj = []  # 第二辆拖船的最优解
                                    tTempBestway = []  # 第二辆拖船的最优解
                                    tTempBestpool = []  # 第二辆拖船的最优解

                                    bestJ = 0  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestWay = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestLossE2 = float('inf')  # 因为又要调用递归函数，所以在这里初始化这四个全局变量
                                    bestPool = []  # 因为又要调用递归函数，所以在这里初始化这四个全局变量

                                    for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                        # print("k=", k)
                                        if haveAny1(i, ways[t]):
                                            if k != len(ways[t]):
                                                k += 1
                                                continue
                                        if k == len(ways[t]):
                                            pass
                                        else:
                                            if ways[t][k] < 2 * orderNumberF:
                                                if k % 2 == 0:
                                                    if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                                else:
                                                    if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                        i - 2 * orderNumberF, 7]:
                                                        continue
                                                    else:
                                                        pass
                                            elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                    i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass
                                            else:
                                                if orderE2[
                                                    ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                        orderE1[
                                                            i - 2 * orderNumberF, 7]:
                                                    continue
                                                else:
                                                    pass

                                        tempSolution = clone(ways[t])
                                        # print("ways[t]=", ways[t])
                                        # print("tempSolution=", tempSolution)
                                        tempSolution.insert(k, i)  # 在此位置插入终点
                                        # print("tempSolution=", tempSolution)
                                        # print("numberTug=", numberTug)
                                        needPoolAll = list(
                                            itertools.combinations(tempPool,
                                                                   numberTug))  # 从pool1中取出needNumber个数
                                        # print("needPoolAll=", needPoolAll)
                                        for p in needPoolAll:
                                            needPools = list(sorted(p, key=lambda i: orderE2[
                                                i - 2 * orderNumberF - orderNumberE1, 9]))
                                            tempBracket = []
                                            tempBracket.append(needPools)
                                            needPools = tempBracket

                                            for needPool in needPools:
                                                # print("needPool=", needPool)
                                                for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                    if haveAny2(i, tempSolution):
                                                        if pos != k:
                                                            pos += 1
                                                            continue
                                                    if pos == k:
                                                        pass
                                                    else:
                                                        if tempSolution[pos] < 2 * orderNumberF:
                                                            if tempSolution[pos] % 2 == 0:
                                                                if orderF[int((tempSolution[pos]) / 2), 5] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    # print(pos)
                                                                    pass
                                                            else:
                                                                if orderF[int((tempSolution[pos]) / 2), 7] < orderE2[
                                                                    needPool[0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                    continue
                                                                else:
                                                                    pass
                                                        elif tempSolution[pos] < 2 * orderNumberF + orderNumberE1:
                                                            if orderE1[tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderE2[tempSolution[
                                                                           pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                    orderE2[
                                                                        needPool[
                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                continue
                                                            else:
                                                                pass

                                                    tempNeedPool = clone(needPool)
                                                    tempTempSolution = clone(tempSolution)
                                                    tempK = k
                                                    tempTempSolution.insert(pos,
                                                                            tempNeedPool[0])  # 插入pool中的第一个元素
                                                    # print("tempTempSolution=",tempTempSolution)
                                                    tempNeedPool.remove(tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                    tempK += 1
                                                    if tempNeedPool != []:
                                                        cirInsert(tempK, tempTempSolution, tempNeedPool, pos, t,
                                                                  needPool)
                                                    else:
                                                        temp = []
                                                        for nub in range(courierNumber):
                                                            temp.append([])
                                                        temp[t] = tempTempSolution
                                                        tempLoss = lossFunction(temp, startTime, printf=False)
                                                        if (tempLoss < bestLossE2):
                                                            # print("bestlossE2=",bestLossE2)
                                                            # print("tempLoss=", tempLoss)

                                                            bestLossE2 = tempLoss
                                                            bestJ = t
                                                            bestWay = clone(tempTempSolution)
                                                            bestPool = clone(needPool)

                                    tempsolution = clone(ways)

                                    for timm in range(len(bTempBestj)):  # bTempBestj、bTempBestway这些都是第一辆拖船的部分最优解
                                        tempsolution[bTempBestj[timm]] = bTempBestway[timm]

                                    tempsolution[bestJ] = bestWay

                                    comNum[i - orderNumberF * 2][t][0] = numberTug

                                    tempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                    tempBestj.append(bestJ)
                                    tempBestway.append(bestWay)
                                    tempBestpool.append(bestPool)

                                    tTempBestloss = tempBestloss  # 第二辆拖船的最优解
                                    tTempBestj = tempBestj  # 第二辆拖船的最优解
                                    tTempBestway = tempBestway  # 第二辆拖船的最优解
                                    tTempBestpool = tempBestpool  # 第二辆拖船的最优解

                                    bestTempTempBestloss = float("inf")  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                    bestTempTempBestj = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                    bestTempTempBestway = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较
                                    bestTempTempBestpool = []  # 用于记录第二辆拖船重用的最优解，最后与tTempBestloss比较

                                    ti = 0

                                    # 考虑重用
                                    for needB in range(numberTug):
                                        nB = needB + 1  # 第一辆拖船第一次访问拉的驳船数
                                        nTug2 = numberTug
                                        nTug2 -= nB  # 第一辆拖船第二次访问拉的驳船数
                                        if (nTug2 == 0) or (nTug2 == 1) or (nB == 1):
                                            continue

                                        for timess in range(maxTravelTime):
                                            if timess == 0:  # 考虑第一次访问的情况

                                                tempTempBestloss = 0  # 用于临时记录第二辆拖船重用的最优解
                                                tempTempBestj = []  # 用于临时记录第二辆拖船重用的最优解
                                                tempTempBestway = []  # 用于临时记录第二辆拖船重用的最优解
                                                tempTempBestpool = []  # 用于临时记录第二辆拖船重用的最优解

                                                bestJ = 0  # 临时存储一下局部最优解
                                                bestWay = []  # 临时存储一下局部最优解
                                                bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                                bestPool = []  # 临时存储一下局部最优解

                                                comNum[i - orderNumberF * 2][t][timess] = nTug2

                                                ttempPool = []  # 用于衔接两次分配任务

                                                for k in range(len(ways[t]) + 1):  # 选择终点插入位置
                                                    # print("k=", k)
                                                    if haveAny1(i, ways[t]):
                                                        if k != len(ways[t]):
                                                            k += 1
                                                            continue
                                                    if k == len(ways[t]):
                                                        pass
                                                    else:
                                                        if ways[t][k] < 2 * orderNumberF:
                                                            if k % 2 == 0:
                                                                if orderF[int((ways[t][k]) / 2), 5] < orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                                    continue
                                                                else:
                                                                    pass
                                                            else:
                                                                if orderF[int((ways[t][k]) / 2), 7] < orderE1[
                                                                    i - 2 * orderNumberF, 7]:
                                                                    continue
                                                                else:
                                                                    pass
                                                        elif ways[t][k] < 2 * orderNumberF + orderNumberE1:
                                                            if orderE1[ways[t][k] - 2 * orderNumberF, 7] < orderE1[
                                                                i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass
                                                        else:
                                                            if orderE2[
                                                                ways[t][k] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                    orderE1[
                                                                        i - 2 * orderNumberF, 7]:
                                                                continue
                                                            else:
                                                                pass

                                                    tempSolution = clone(ways[t])
                                                    # print("ways[j]=", ways[j])
                                                    # print("tempSolution=", tempSolution)
                                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                                    # print("tempSolution=", tempSolution)
                                                    # print("bNum=", bNum)
                                                    needPoolAll = list(
                                                        itertools.combinations(tempPool,
                                                                               nTug2))  # 从pool1中取出needNumber个数
                                                    # print("needPoolAll=", needPoolAll)
                                                    for p in needPoolAll:
                                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                                        tempBracket = []
                                                        tempBracket.append(needPools)
                                                        needPools = tempBracket

                                                        for needPool in needPools:
                                                            # print("needPool=", needPool)
                                                            for pos in range(0, k + 1):  # pos是pool中第一个元素要插入的位置
                                                                if haveAny2(i, tempSolution):
                                                                    if pos != k:
                                                                        pos += 1
                                                                        continue
                                                                if pos == k:
                                                                    pass
                                                                else:
                                                                    if tempSolution[pos] < 2 * orderNumberF:
                                                                        if tempSolution[pos] % 2 == 0:
                                                                            if orderF[int((tempSolution[pos]) / 2), 5] < \
                                                                                    orderE2[
                                                                                        needPool[
                                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                                continue
                                                                            else:
                                                                                # print(pos)
                                                                                pass
                                                                        else:
                                                                            if orderF[int((tempSolution[pos]) / 2), 7] < \
                                                                                    orderE2[
                                                                                        needPool[
                                                                                            0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                                continue
                                                                            else:
                                                                                pass
                                                                    elif tempSolution[
                                                                        pos] < 2 * orderNumberF + orderNumberE1:
                                                                        if orderE1[
                                                                            tempSolution[pos] - 2 * orderNumberF, 7] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass
                                                                    else:
                                                                        if orderE2[tempSolution[
                                                                                       pos] - 2 * orderNumberF - orderNumberE1, 9] < \
                                                                                orderE2[
                                                                                    needPool[
                                                                                        0] - 2 * orderNumberF - orderNumberE1, 9]:
                                                                            continue
                                                                        else:
                                                                            pass

                                                                tempNeedPool = clone(needPool)
                                                                tempTempSolution = clone(tempSolution)
                                                                tempK = k
                                                                tempTempSolution.insert(pos, tempNeedPool[
                                                                    0])  # 插入pool中的第一个元素
                                                                # print("tempTempSolution=",tempTempSolution)
                                                                tempNeedPool.remove(
                                                                    tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                                tempK += 1
                                                                if tempNeedPool != []:
                                                                    cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                              pos, t,
                                                                              needPool)
                                                                else:
                                                                    temp = []
                                                                    for nub in range(courierNumber):
                                                                        temp.append([])
                                                                    temp[t] = tempTempSolution
                                                                    tempLoss = lossFunction(temp, startTime,
                                                                                            printf=False)

                                                                    if (tempLoss < bestLossE2):
                                                                        # print("bestlossE2=",bestLossE2)
                                                                        # print("tempLoss=", tempLoss)

                                                                        bestLossE2 = tempLoss
                                                                        bestJ = t
                                                                        bestWay = clone(tempTempSolution)
                                                                        bestPool = clone(needPool)

                                                tempTempBestj.append(bestJ)
                                                tempTempBestway.append(bestWay)
                                                tempTempBestpool.append(bestPool)

                                                comNum[i - orderNumberF * 2][t][timess] = nTug2

                                                ttempPool = clone(tempPool)
                                                for point in bestPool:
                                                    tempPoint = point
                                                    ttempPool.remove(tempPoint)

                                                continue

                                            if timess == 1:  # 考虑第二次访问的情况

                                                comNum[i - orderNumberF * 2][t][timess] = nB

                                                bestJ = 0  # 临时存储一下局部最优解
                                                bestWay = []  # 临时存储一下局部最优解
                                                bestLossE2 = float('inf')  # 临时存储一下局部最优解
                                                bestPool = []  # 临时存储一下局部最优解

                                                for k in range(len(tempTempBestway[0]) + 1):  # 选择终点插入位置
                                                    couldPos = tempTempBestway[0].index(i)
                                                    if k <= couldPos:
                                                        continue
                                                    # print("k=", k)
                                                    tempSolution = clone(tempTempBestway[0])
                                                    # print("ways[t]=", ways[t])
                                                    # print("tempSolution=", tempSolution)
                                                    tempSolution.insert(k, i)  # 在此位置插入终点
                                                    # print("tempSolution=", tempSolution)
                                                    # print("numberTug=", numberTug)
                                                    needPoolAll = list(
                                                        itertools.combinations(ttempPool,
                                                                               nB))  # 从pool1中取出needNumber个数
                                                    # print("needPoolAll=", needPoolAll)
                                                    for p in needPoolAll:
                                                        needPools = list(sorted(p, key=lambda i: orderE2[
                                                            i - 2 * orderNumberF - orderNumberE1, 9]))
                                                        tempBracket = []
                                                        tempBracket.append(needPools)
                                                        needPools = tempBracket

                                                        for needPool in needPools:
                                                            # print("needPool=", needPool)
                                                            for pos in range(couldPos + 1,
                                                                             k + 1):  # pos是pool中第一个元素要插入的位置
                                                                tempNeedPool = clone(needPool)
                                                                tempTempSolution = clone(tempSolution)
                                                                tempK = k
                                                                tempTempSolution.insert(pos, tempNeedPool[
                                                                    0])  # 插入pool中的第一个元素
                                                                # print("tempTempSolution=",tempTempSolution)
                                                                tempNeedPool.remove(
                                                                    tempNeedPool[0])  # 从tempPool中移除第一个元素
                                                                tempK += 1
                                                                if tempNeedPool != []:
                                                                    cirInsert(tempK, tempTempSolution, tempNeedPool,
                                                                              pos, t,
                                                                              needPool)
                                                                else:
                                                                    temp = []
                                                                    for nub in range(courierNumber):
                                                                        temp.append([])
                                                                    temp[t] = tempTempSolution
                                                                    tempLoss = lossFunction(temp, startTime,
                                                                                            printf=False)
                                                                    if (tempLoss < bestLossE2):
                                                                        # print("bestlossE2=",bestLossE2)
                                                                        # print("tempLoss=", tempLoss)

                                                                        bestLossE2 = tempLoss
                                                                        bestJ = t
                                                                        bestWay = clone(tempTempSolution)
                                                                        bestPool = clone(needPool)

                                                tempsolution = clone(ways)
                                                tempsolution[bestJ] = bestWay

                                                comNum[i - orderNumberF * 2][t][0] = nTug2
                                                comNum[i - orderNumberF * 2][t][1] = nB

                                                tempTempBestloss = lossFunction(tempsolution, startTime, printf=False)
                                                tempTempBestj.append(bestJ)
                                                tempTempBestway.append(bestWay)  # 这里的tempBestway比较特殊但是不影响操作
                                                tempTempBestpool.append(bestPool)

                                                if ti == 0:
                                                    bestTempTempBestloss = tempTempBestloss
                                                    bestTempTempBestj = tempTempBestj
                                                    bestTempTempBestway = tempTempBestway
                                                    bestTempTempBestpool = tempTempBestpool
                                                    ti += 1
                                                else:
                                                    if bestTempTempBestloss > tempTempBestloss:
                                                        bestTempTempBestloss = tempTempBestloss
                                                        bestTempTempBestj = tempTempBestj
                                                        bestTempTempBestway = tempTempBestway
                                                        bestTempTempBestpool = tempTempBestpool
                                                    ti += 1

                                    if bestTempTempBestloss < tTempBestloss:
                                        tTempBestloss = bestTempTempBestloss
                                        tTempBestj = bestTempTempBestj  # [a,b]
                                        tTempBestway = bestTempTempBestway  # [[a,b],[c,d]]
                                        tTempBestpool = bestTempTempBestpool  # [[a,b],[c,d]]

                                    # 至此，第一辆拖船以及第二辆拖船的最优部分解均已经收集完毕

                                    for leng in range(len(tTempBestj)):
                                        bTempBestj.append(tTempBestj[leng])
                                        bTempBestway.append(tTempBestway[leng])
                                        bTempBestpool.append(tTempBestpool[leng])

                                    tempsolution = clone(ways)

                                    for leng in range(len(bTempBestj)):
                                        tempsolution[bTempBestj[leng]] = bTempBestway[leng]

                                    tNum = []

                                    for ii in range(orderNumberE1):
                                        tNum.append([])
                                        for jj in range(courierNumber):
                                            tNum[ii].append([])
                                            for _ in range(2):
                                                tNum[ii][jj].append(0)

                                    comNum[i - orderNumberF * 2] = tNum[i - orderNumberF * 2]

                                    tempp = []
                                    for to in bTempBestj:
                                        if to in tempp:
                                            # 查找 to 在 bbestTempBestj 中第二次出现的位置
                                            index = find_nth_occurrence(bTempBestj, to, 2)
                                            if index != -1:
                                                comNum[i - orderNumberF * 2][to][1] = len(bTempBestpool[index])
                                        else:
                                            index = bTempBestj.index(to)
                                            comNum[i - orderNumberF * 2][to][0] = len(bTempBestpool[index])
                                        tempp.append(to)

                                    tempBestloss = lossFunction(tempsolution, startTime, printf=False)

                                    if tii == 0:
                                        bbestTempBestloss = tempBestloss
                                        bbestTempBestj = bTempBestj
                                        bbestTempBestway = bTempBestway
                                        bbestTempBestpool = bTempBestpool
                                        tii += 1
                                    elif tii == 1:
                                        if tempBestloss < bbestTempBestloss:
                                            bbestTempBestlosss = bbestTempBestloss

                                            bbestTempBestloss = tempBestloss
                                            bbestTempBestj = bTempBestj
                                            bbestTempBestway = bTempBestway
                                            bbestTempBestpool = bTempBestpool
                                        else:
                                            bbestTempBestlosss = tempBestloss
                                        tii += 1
                                    else:
                                        tii += 1
                                        if (bbestTempBestloss < tempBestloss) and (bbestTempBestlosss > tempBestloss):
                                            bbestTempBestlosss = tempBestloss
                                        elif (tempBestloss < bbestTempBestloss):
                                            bbestTempBestlosss = bbestTempBestloss

                                            bbestTempBestloss = tempBestloss
                                            bbestTempBestj = bTempBestj
                                            bbestTempBestway = bTempBestway
                                            bbestTempBestpool = bTempBestpool
                    if (bbestTempBestlosss - bbestTempBestloss) < BestLoss:
                        BestLoss = bbestTempBestlosss - bbestTempBestloss
                        BestJ = bbestTempBestj
                        BestWay = bbestTempBestway
                        BestPool = bbestTempBestpool

            for chosenone in range(len(BestJ)):
                ways[BestJ[chosenone]] = BestWay[chosenone]
                for point in BestPool[chosenone]:
                    tempPoint = point
                    pool1.remove(tempPoint)
            tNum = []

            for ii in range(orderNumberE1):
                tNum.append([])
                for jj in range(courierNumber):
                    tNum[ii].append([])
                    for _ in range(2):
                        tNum[ii][jj].append(0)

            comNum[i - orderNumberF * 2] = tNum[i - orderNumberF * 2]

            tempp = []
            for to in BestJ:
                if to in tempp:
                    # 查找 to 在 bbestTempBestj 中第二次出现的位置
                    index = find_nth_occurrence(BestJ, to, 2)
                    if index != -1:
                        comNum[i - orderNumberF * 2][to][1] = len(BestPool[index])
                else:
                    index = BestJ.index(to)
                    comNum[i - orderNumberF * 2][to][0] = len(BestPool[index])
                tempp.append(to)

    pool1 = []
    pool2 = []
    return ways, pool1, pool2


######################################################


def find_last(way,target):
    way_new=[i for i in way if i >=orderNumberF*2 and i <orderNumberF*2+orderNumberE1]
    temppool=[]
    for which in range(len(way_new)):

        if  which==0:
            if way_new[which]==target:
                return None
            else:
                last=way_new[which]
        else:
            if way_new[which]==target:
                if last not in temppool:
                    return way.index(last)
                else:
                    return  find_nth_occurrence(way, last, 2)
            else:
                temppool.append(last)
                last=way_new[which]

##################E类型订单摧毁算子######################
# 随机摧毁E类型订单的某路径
def destroyE1(ways, step=2):  # 随机摧毁算子
    pool1 = []
    pool2 = []
    tempComNum = []
    for i in range(step):
        index1 = random.randint(0, len(ways) - 1)  # 在0到2双闭区间生成一个整数
        while (len(ways[index1]) < 1):  # 看看有没有那种空解，有的话重新生成一个
            index1 = random.randint(0, len(ways) - 1)  # 在0到2双闭区间生成一个整数
        index2 = random.randint(0, len(ways[index1]) - 1)  # 在0到len(ways[index1]) - 1双闭区间生成一个整数
        x = ways[index1][index2]

        while (x <= orderNumberF * 2 - 1) or (x >= orderNumberF * 2 + orderNumberE1):
            index1 = random.randint(0, len(ways) - 1)  # 在0到3双闭区间生成一个整数
            while (len(ways[index1]) < 1):  # 看看有没有那种空解，有的话重新生成一个
                index1 = random.randint(0, len(ways) - 1)  # 在0到3双闭区间生成一个整数
            index2 = random.randint(0, len(ways[index1]) - 1)  # 在0到len(ways[index1]) - 1双闭区间生成一个整数
            x = ways[index1][index2]
        if x <= orderNumberF * 2 + orderNumberE1 - 1:
            tempComNum = comNum[x - 2 * orderNumberF]
            for times in range(len(tempComNum)):
                for timess in range(maxTravelTime):
                    number_removed = tempComNum[times][timess]
                    if number_removed == 0:
                        continue
                    else:
                        way = [i for i in ways[times] if i >= orderNumberF * 2 + orderNumberE1]
                    index2 = ways[times].index(x)
                    if number_removed == len(way):
                        for i in way:
                            # print("i=",i)
                            ways[times].remove(i)
                            pool1.append(i)
                    else:
                        tempway = way.copy()
                        for i in way:
                            g = ways[times].index(i)
                            if g > index2:
                                h = ways[times][g]
                                # print("h=", h)
                                tempway.remove(h)
                        index_first = find_last(ways[times], x)
                        if index_first != None:
                            for z in way:
                                p = ways[times].index(z)
                                if p < index_first:
                                    rr = ways[times][p]
                                    tempway.remove(rr)
                        way = tempway
                        way1 = [i for i in ways[times] if
                                (i >= orderNumberF * 2) and (i < orderNumberF * 2 + orderNumberE1)]

                        if number_removed == len(way):
                            pass
                        else:
                            # 先得到在p点之前的E类型订单终点，并存储到tempway中
                            temppool = []
                            tempway = copy.deepcopy(way1)
                            for order in way1:
                                if order not in temppool:
                                    t = ways[times].index(order)
                                    if t >= index2:
                                        e = ways[times][t]
                                        tempway.remove(e)
                                    temppool.append(order)
                                else:
                                    t = find_nth_occurrence(ways[times], order, 2)
                                    if t >= index2:
                                        e = ways[times][t]
                                        tempway.remove(e)

                            # 根据每个点的comNum对各个数进行排查
                            temppool = []
                            comnum = 0  # comnum是需要移除的空拖船数量
                            for dest in tempway:
                                if dest not in temppool:
                                    comnum += comNum[dest - 2 * orderNumberF][times][0]
                                    temppool.append(dest)
                                else:
                                    comnum += comNum[dest - 2 * orderNumberF][times][1]

                            # 开始移除
                            way = copy.deepcopy(way[comnum:])
                        for i in range(number_removed):
                            index = random.randint(0, len(way) - 1)
                            j = way[index]
                            ways[times].remove(j)
                            way.remove(j)
                            pool1.append(j)
                    ways[times].remove(x)
                    if (x not in pool2):
                        pool2.append(x)

            nested_list = clone_runoob(ways)
            flat_list = []
            for sublist in nested_list:
                flat_list.extend(sublist)
            for i in pool1:
                flat_list.append(i)
            for i in pool2:
                flat_list.append(i)
            flatList = [i for i in flat_list if i >= orderNumberF * 2 + orderNumberE1]

            for i in range(orderNumberE2):
                if not ((i + orderNumberF * 2 + orderNumberE1) in flatList):
                    pool1.append(i + orderNumberF * 2 + orderNumberE1)
    return ways, pool1, pool2


# 贪婪摧毁E类型订单的某路径 #注意有BestLoss所在路径中没有E类型订单的点的情况
def destroyE2(ways, step=2):  # 较差摧毁算子
    pool1 = []
    pool2 = []
    worstLoss = 0
    worstc = 0
    woworstc = 0
    # print("len(ways)=",len(ways))

    for _ in range(step):
        for i in range(len(ways)):
            temp = []
            for nub in range(courierNumber):
                temp.append([])
            temp[i] = ways[i]
            # print("temp=", temp)
            # print("ways[i]=",ways[i])
            nowLoss = lossFunction(temp, startTime, printf=False)  # startTime是全局变量 #相当于是按顺序把拖船的任务顺序放进去
            # print("nowloss=",nowLoss)
            # print("worstLoss=", worstLoss)
            if (nowLoss > worstLoss):
                worstLoss = nowLoss
                woworstc = worstc
                worstc = i
                # print("worstc=",worstc)#把loss最大拖船丢进tempp

        tempp = []
        for nubb in range(courierNumber):
            tempp.append([])
        tempp[worstc] = ways[worstc]

        # print("ways[worstc]=",ways[worstc])
        # print("ways[woworstc]=", ways[woworstc])
        bestLoss = lossFunction(tempp, startTime, printf=False)  # loss最大的拖船对应的loss，称之为bestLoss

        way1 = [i for i in ways[worstc] if (i >= orderNumberF * 2) and (i < orderNumberF * 2 + orderNumberE1)]

        worstoList = []
        worstoPoint = -1
        tempbowl = []
        whichtimes = -1
        times = -1
        for p in way1:
            way2 = [i for i in ways[worstc] if i >= orderNumberF * 2 + orderNumberE1]
            if p not in tempbowl:
                needNumber = comNum[p - 2 * orderNumberF][worstc][0]
                times = 0
                tempbowl.append(p)

            else:
                needNumber = comNum[p - 2 * orderNumberF][worstc][1]
                times = 1
            tempway = way2.copy()

            if times == 0:
                index = ways[worstc].index(p)
            elif times == 1:
                index = find_nth_occurrence(ways[worstc], p, 2)

            for point in way2:
                g = ways[worstc].index(point)
                if g > index:
                    h = ways[worstc][g]
                    tempway.remove(h)
            index_first = find_last(ways[worstc], point)
            if index_first != None:
                for z in way2:
                    p = ways[worstc].index(z)
                    if p < index_first:
                        rr = ways[worstc][p]
                        tempway.remove(rr)
            way2 = tempway

            if needNumber == len(way2):
                pass
            else:
                # 先得到在p点之前的E类型订单终点，并存储到tempway中
                temppool = []
                tempway = copy.deepcopy(way1)
                for order in way1:
                    if order not in temppool:
                        t = ways[worstc].index(order)
                        if t >= index:
                            e = ways[worstc][t]
                            tempway.remove(e)
                        temppool.append(order)
                    else:
                        t = find_nth_occurrence(ways[worstc], order, 2)
                        if t >= index:
                            e = ways[worstc][t]
                            tempway.remove(e)

                # 根据每个点的comNum对各个数进行排查
                temppool = []
                comnum = 0  # comnum是需要移除的空拖船数量
                for dest in tempway:
                    if dest not in temppool:
                        comnum += comNum[dest - 2 * orderNumberF][worstc][0]
                        temppool.append(dest)
                    else:
                        comnum += comNum[dest - 2 * orderNumberF][worstc][1]

                # 开始移除
                way2 = copy.deepcopy(way2[comnum:])

            result = list(itertools.combinations(way2, needNumber))
            # print("result=",result)
            for sublist in result:
                tempSolution = clone(ways[worstc])
                for i in sublist:
                    tempSolution.remove(i)
                l = p
                tempSolution.remove(l)
                temp = []
                for nu in range(courierNumber):
                    temp.append([])
                temp[worstc] = tempSolution
                tempLoss = lossFunction(temp, startTime, printf=False)  # 改成True看看，原本是False
                if (tempLoss < bestLoss):
                    bestLoss = tempLoss
                    worstoList = sublist
                    worstoPoint = l
                    whichtimes = times

        # print("ways[worstc]=", ways[worstc])
        for i in worstoList:
            ways[worstc].remove(i)
            pool1.append(i)
        if worstoPoint != -1:
            comNum[worstoPoint - 2 * orderNumberF][worstc][whichtimes] = 0

            if whichtimes == 0:
                ways[worstc].remove(worstoPoint)
            elif whichtimes == 1:
                tempway = ways[worstc][::-1]
                tempway.remove(worstoPoint)
                ways[worstc] = tempway[::-1]
            pool2.append(worstoPoint)

            for times in range(len(comNum[worstoPoint - 2 * orderNumberF])):
                for timess in range(maxTravelTime):
                    number_removed = comNum[worstoPoint - 2 * orderNumberF][times][timess]
                    if number_removed == 0:
                        continue
                    else:
                        way = [i for i in ways[times] if i >= orderNumberF * 2 + orderNumberE1]
                    index2 = ways[times].index(worstoPoint)
                    if number_removed == len(way):
                        for i in way:
                            # print("i=",i)
                            ways[times].remove(i)
                            pool1.append(i)
                    else:
                        tempway = way.copy()
                        for i in way:
                            g = ways[times].index(i)
                            if g > index2:
                                h = ways[times][g]
                                # print("h=", h)
                                tempway.remove(h)
                        index_first = find_last(ways[times], worstoPoint)
                        if index_first != None:
                            for z in way:
                                p = ways[times].index(z)
                                if p < index_first:
                                    rr = ways[times][p]
                                    tempway.remove(rr)
                        way = tempway
                        for i in range(number_removed):
                            index = random.randint(0, len(way) - 1)
                            j = way[index]
                            ways[times].remove(j)
                            way.remove(j)
                            pool1.append(j)
                    ways[times].remove(worstoPoint)
                    if (worstoPoint not in pool2):
                        pool2.append(worstoPoint)

            nested_list = clone_runoob(ways)
            flat_list = []
            for sublist in nested_list:
                flat_list.extend(sublist)
            for i in pool1:
                flat_list.append(i)
            for i in pool2:
                flat_list.append(i)
            flatList = [i for i in flat_list if i >= orderNumberF * 2 + orderNumberE1]

            for i in range(orderNumberE2):
                if not ((i + orderNumberF * 2 + orderNumberE1) in flatList):
                    pool1.append(i + orderNumberF * 2 + orderNumberE1)

    return ways, pool1, pool2

######################################################


# 公式计算两点间距离（m）
def geoDistance(point1, point2):
    lng1, lat1, lng2, lat2 = map(radians, [targetPoint[point1][0], targetPoint[point1][1],
                                           targetPoint[point2][0], targetPoint[point2][1]])  # 经纬度转换成弧度
    dlon = lng2 - lng1
    dlat = lat2 - lat1
    a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
    distance = 2 * asin(sqrt(a)) * 6371 * 1000  # 地球平均半径，6371004m
    distance = round(distance, 0)
    return distance


# 用于计算时间差
def timeLossF(nowTime, promisTime):
    if (nowTime < promisTime):
        d = promisTime - nowTime
    else:
        d = nowTime - promisTime
    return d


# 用于计算损失
def lossFunction(ways, startTime, printf=False):
    global comNum

    timeloss = 0
    distance = 0
    overtimeloss = 0
    caploss = 0
    no = 0

    # print("ways=",ways)
    for way in ways:
        tempcap = []
        cap = 0
        # print("cap=", cap)
        time = startTime  # 令time=startTime
        lastPoint = -1
        # print("way=",way)
        for point in way:
            # print("当前计算loss的point=",point)
            if (lastPoint != -1):
                # print("lastPoint=",lastPoint)
                # print("point=",point)
                distance += geoDistance(lastPoint, point)
                # print("距离是",geoDistance(lastPoint,point))
                time += int(geoDistance(lastPoint, point) / v)
                # print("用时",int(geoDistance(lastPoint,point)/v))
            lastPoint = point
            if (printf):
                print("到达目标点", point, "时，是", time)

            if point < orderNumberF * 2:
                if (point % 2 == 0):  # 判断是不是起点
                    cap += 1
                    # print("cap=", cap)
                    if cap > maxcap:
                        # print("overcap")
                        caploss += 1  # caploss等于非0的任何正数对于loss来讲都是致命的
                    if (time == startTime):  # 判断是不是每个拖船要取的第一个驳船，若是则无需在此处计算loss，并且令time等于最晚的pick_time
                        time = orderF[int(point / 2)][pick_time_late]
                    else:
                        if (time < orderF[int(point / 2)][pick_time_early]):
                            P = timeLossF(time, orderF[int(point / 2)][pick_time_early])
                            overtimeloss += timeLossF(time, orderF[int(point / 2)][pick_time_early])
                            time = orderF[int(point / 2)][pick_time_late]
                            if (P > 0 and printf):
                                print("目标点", point, "早时", P, "秒")
                        elif (time > orderF[int(point / 2)][pick_time_late]):
                            G = timeLossF(time, orderF[int(point / 2)][pick_time_late])
                            overtimeloss += timeLossF(time, orderF[int(point / 2)][pick_time_late])
                            time += 300  # 5 min 固定驳船时间
                            if (G > 0 and printf):
                                print("目标点", point, "晚时", G, "秒")
                        else:
                            if (time + 300) > orderF[int(point / 2)][pick_time_late]:
                                time += 300
                            else:
                                time = orderF[int(point / 2)][pick_time_late]
                            if (printf):
                                print("目标点", point, "按时到达")

                else:  # 判断是不是终点 zhuwenxishixiaobaba
                    if (time == startTime):
                        print("point=", point)
                        print("wrong in lossFunction（<F*2且为终点）")
                        sys.exit(0)
                    else:
                        cap -= 1
                        # print("cap=", cap)

                        if (time < orderF[int(point / 2)][deliver_time_early]):
                            P = timeLossF(time, orderF[int(point / 2)][deliver_time_early])
                            overtimeloss += timeLossF(time, orderF[int(point / 2)][deliver_time_early])
                            time = orderF[int(point / 2)][pick_time_late]
                            if (P > 0 and printf):
                                print("目标点", point, "早时", P, "秒")
                        elif (time > orderF[int(point / 2)][deliver_time_late]):
                            G = timeLossF(time, orderF[int(point / 2)][deliver_time_late])
                            overtimeloss += timeLossF(time, orderF[int(point / 2)][deliver_time_late])
                            time += 300  # 5min 取消固定驳船时间
                            if (G > 0 and printf):
                                print("目标点", point, "晚时", G, "秒")
                        else:
                            if (time + 300) > orderF[int(point / 2)][deliver_time_late]:
                                time += 300
                            else:
                                time = orderF[int(point / 2)][deliver_time_late]
                            if (printf):
                                print("目标点", point, "按时到达")

            elif point < orderNumberF * 2 + orderNumberE1:
                son = 0
                if (time == startTime):
                    print("point=", point)
                    print("time=", time)
                    print("wrong in lossFunction（<F*2+E1）")
                    sys.exit(0)
                else:
                    # print(comNum)
                    if point in tempcap:
                        son = 1
                    else:
                        tempcap.append(point)
                    cap -= comNum[point - orderNumberF * 2][no][son]
                    # print("cap=", cap)
                    # print("no=", no)
                    if (time < orderE1[point - orderNumberF * 2][deliver_time_early]):
                        P = timeLossF(time, orderE1[point - orderNumberF * 2][deliver_time_early])
                        overtimeloss += timeLossF(time, orderE1[point - orderNumberF * 2][deliver_time_early])
                        time = orderE1[point - orderNumberF * 2][deliver_time_early]
                        time += 300
                        if (P > 0 and printf):
                            print("目标点", point, "早时", P, "秒")
                    elif (time > orderE1[point - orderNumberF * 2][deliver_time_late]):
                        G = timeLossF(time, orderE1[point - orderNumberF * 2][deliver_time_late])
                        overtimeloss += timeLossF(time, orderE1[point - orderNumberF * 2][deliver_time_late])
                        time += 300  # 5 min 取消固定驳船时间
                        if (G > 0 and printf):
                            print("目标点", point, "晚时", G, "秒")
                    else:
                        time += 300  # 到了之后转头立马走
                        if (printf):
                            print("目标点", point, "按时到达")

            elif point < orderNumberF * 2 + orderNumberE1 + orderNumberE2:
                if (time < orderE2[point - orderNumberF * 2 - orderNumberE1][free_time]):
                    time = orderE2[point - orderNumberF * 2 - orderNumberE1][free_time]
                cap += 1
                # print("cap=", cap)
                if cap > maxcap:
                    # print("overcap")
                    caploss += 1  # caploss等于非0的任何正数对于loss来讲都是致命的
                time += 300  # 5 min 固定驳船时间
        no += 1
        timeloss += time - startTime
    # print("timeloss=",timeloss)
    # print("distance=", distance)
    # print("overtimeloss=", overtimeloss)
    # print("caploss=", caploss)
    x = 1
    y = 1000000000000000000000000  # 超载损失的系数
    z = 10000000000000000  # 超时损失的系数
    loss = x * timeloss + x * distance + y * caploss + z * overtimeloss
    return loss


# 程序最初开始执行的地方
loss = list()
Bsolution = list()
current_loss = list()
startTime = 0
loss_list = list()

# 数组索引设置
pick_time_early = 4
pick_time_late = 5
deliver_time_early = 6
deliver_time_late = 7
need_deliver_amount = 8
free_time = 9

# 读取全部订单
path = "inlandriver_order/order6.csv"
orderall = pd.read_csv(path, header=0, usecols=["pick_lng", "pick_lat", "custome_lng", "custome_lat",
                                                "pick_time_early", "pick_time_late", "deliver_time_early",
                                                "deliver_time_late", "need_deliver_amount", "free_time"])
orderF = orderall[orderall["pick_time_late"].notnull()]
orderE1 = orderall[orderall["need_deliver_amount"].notnull()]
orderE2 = orderall[orderall["free_time"].notnull()]

# 读取F类型订单
orderF = orderF.values
orderF = np.array(orderF)  # 将dataframe类型的数据保存为numpy数组

# 读取E类型订单的终点部分
orderE1 = orderE1.values
orderE1 = np.array(orderE1)  # 将dataframe类型的数据保存为numpy数组

# 读取E类型订单的起点部分
orderE2 = orderE2.values
orderE2 = np.array(orderE2)  # 将dataframe类型的数据保存为numpy数组

# 模型设置参数
orderNumberF = np.shape(orderF)[0]
orderNumberE1 = np.shape(orderE1)[0]
orderNumberE2 = np.shape(orderE2)[0]
OrderNumber = orderNumberF + orderNumberE1  # OrderNumber为总单数
pointNumber = orderNumberF * 2 + orderNumberE1 + orderNumberE2  # pointNumber后面将用于读取所有的目标点

# 设置拖船信息
courierNumber = 3  # 拖船数量为3
v = 5  # 5m/s = 18km/h = 10节 为拖船的平均速度
maxcap = 5  # 拖船的最大容载量，所以理论上来讲一个E类型订单需要的驳船最好不要超过10个，甚至要小于十个
maxTugNum = 2  # 最多允许几个拖船共同完成一个E类型订单任务
maxTravelTime = 2  # 最多允许同一个拖船访问几次一个点

tugs = list(range(courierNumber))

comNum = []

for ii in range(orderNumberE1):
    comNum.append([])
    for jj in range(courierNumber):
        comNum[ii].append([])
        for _ in range(2):
            comNum[ii][jj].append(0)

bestComNum = []

for ii in range(orderNumberE1):
    bestComNum.append([])
    for jj in range(courierNumber):
        bestComNum[ii].append([])
        for _ in range(2):
            bestComNum[ii][jj].append(0)

bestcomnum = []

for ii in range(orderNumberE1):
    bestcomnum.append([])
    for jj in range(courierNumber):
        bestcomnum[ii].append([])
        for _ in range(2):
            bestcomnum[ii][jj].append(0)

tempcomnum = []

for ii in range(orderNumberE1):
    tempcomnum.append([])
    for jj in range(courierNumber):
        tempcomnum[ii].append([])
        for _ in range(2):
            tempcomnum[ii][jj].append(0)

firstcomnum = []

for ii in range(orderNumberE1):
    firstcomnum.append([])
    for jj in range(courierNumber):
        firstcomnum[ii].append([])
        for _ in range(2):
            firstcomnum[ii][jj].append(0)

# 读取F类型订单目标点信息#用ndarray是因为在这里用嵌套列表会显示越界
targetPoint = np.empty([pointNumber, 2], dtype=float)  # 生成了一个42*2的多维数组，一列存点的经度，一列存点的纬度
j = 0
for i in range(orderNumberF):  # 读取各起终点的经纬度
    targetPoint[j][0] = orderF[i][0]
    targetPoint[j][1] = orderF[i][1]
    j += 1
    targetPoint[j][0] = orderF[i][2]
    targetPoint[j][1] = orderF[i][3]
    j += 1

# 读取E类型订单起点信息
for i in range(orderNumberE1):
    targetPoint[j][0] = orderE1[i][2]
    targetPoint[j][1] = orderE1[i][3]
    j += 1

# 读取E类型订单终点信息
for i in range(orderNumberE2):
    targetPoint[j][0] = orderE2[i][0]
    targetPoint[j][1] = orderE2[i][1]
    j += 1

# 构建解的框架
ways = []
for i in range(courierNumber):  # courierNumber为拖船数量
    ways.append([])
    # 第 0 次 [[]]
    # 第 1 次 [[], []]
    # 第 2 次 [[], [], []]
    # 第 3 次 [[], [], [], []]
    # 第 4 次 [[], [], [], [], []]

# 利用贪婪算法初始化解
# 准备pool
pool = []
for i in range(pointNumber):
    pool.append(i)
# print("pool=",pool)

# 初始化F类型订单
time_1 = time.time()
ways = genF(ways, pool)
print("初始化F类型订单后的ways等于", ways)

# 初始化E类型订单
pool = [i for i in pool if i >= orderNumberF * 2]
# print("pool=",pool)
pool1 = [i for i in pool if i >= orderNumberF * 2 + orderNumberE1]
# print("pool1=",pool1)
pool2 = [i for i in pool if i < orderNumberF * 2 + orderNumberE1]
# print("pool2=",pool2)
ways = genE(ways, pool1, pool2)
time_2 = time.time()
print("初始化E类型订单后的ways等于", ways)

Bsolution.append(ways)
# loss_list.append(lossFunction(ways, startTime))
firstcomnum = copy.deepcopy(comNum)
loss.append(lossFunction(ways, startTime))  # 这里的comNum是对的
# print("Bsolution=",Bsolution)
# print("loss=",loss)

# 算法参数设置
T = 100  # 初始温度
a = 0.98  # 退火系数
b = 0.5
endF = 29
iterxMax = 10  # 最大迭代次数
countMax = 150  # 最优解多少次没有更新就停止迭代
counter = 0  # 用于记录最优解几次没有更新了

wDestroyF = [1 for i in range(2)]  # [1,1] # weights of the destroy operators  #摧毁算子的权重
wRepairF = [1 for i in range(3)]  # [1,1] # weights of the repair operators #修复算子的权重
destroyUseTimesF = [0 for i in range(2)]  # [0,0] #The number of times the destroy operator has been used #摧毁算子的使用次数
repairUseTimesF = [0 for i in range(3)]  # [0,0] #The number of times the repair operator has been used #修复算子的使用次数
destroyScoreF = [1 for i in range(2)]  # [1,1] # the score of destroy operators #摧毁算子的得分
repairScoreF = [1 for i in range(3)]  # [1,1] # the score of repair operators #修复算子的得分

wDestroyE = [1 for i in range(2)]  # [1,1] # weights of the destroy operators  #摧毁算子的权重
wRepairE = [1 for i in range(3)]  # [1,1] # weights of the repair operators #修复算子的权重
destroyUseTimesE = [0 for i in range(2)]  # [0,0] #The number of times the destroy operator has been used #摧毁算子的使用次数
repairUseTimesE = [0 for i in range(3)]  # [0,0] #The number of times the repair operator has been used #修复算子的使用次数
destroyScoreE = [1 for i in range(2)]  # [1,1] # the score of destroy operators #摧毁算子的得分
repairScoreE = [1 for i in range(3)]  # [1,1] # the score of repair operators #修复算子的得分

solution = ways  # initial solution
bestcomnum = copy.deepcopy(comNum)  # 用bestcomnum记录solution的分配情况

bestSolution = clone_runoob(solution)  # 套用了一个防止产生指针的复制的函数来进行复制
bestComNum = copy.deepcopy(comNum)  # 存住bestSolution的分配情况

############################################
# 增加新机制：最优解n次不迭代就终止
############################################

iterx = 0
while iterx < iterxMax:  # while stop criteria not met
    print("----------------------------------------------------------------")
    print("正在进行第", iterx + 1, "次迭代，共", iterxMax, "次")
    print("----------------------------------------------------------------")
    if iterx <= 0:
        stepF = 15
        stepE = 2
    else:
        stepF = 2
        stepE = 1
    while T > 10:
        r = random.randint(0, endF)
        bestcomnum = copy.deepcopy(comNum)  # 更新bestcomnum
        if r != endF:
            print("r=", r + 1)
            # print("solution(before=",solution)
            # print("comNum(before=",comNum)
            destroyedSolution, remove, destroyOperatorIndexF = selectAndUseDestroyOperator(wDestroyF, solution, stepF)
            # print("solution(mid=", destroyedSolution)
            # print("comNum(mid=", comNum
            newSolution, repairOperatorIndexF, remove = selectAndUseRepairOperator(wRepairF, destroyedSolution, remove)
            # print("solution(after=", newSolution)
            # print("comNum(after=", comNum)
        elif r == endF:
            print("r=", r + 1)
            # print("solution(before=", solution)
            # print("comNum(before=", comNum)
            destroyedSolution, remove1, remove2, destroyOperatorIndexE = selectAndUseDestroyOperator2(wDestroyE,solution,stepE)
            # print("solution(mid=", destroyedSolution)
            # print("comNum(mid=", comNum)
            newSolution, repairOperatorIndexE, remove1, remove2 = selectAndUseRepairOperator2(wRepairE,destroyedSolution,remove1, remove2)
            # print("solution(after=", newSolution)
            # print("comNum(after=", comNum)
        current_loss.append(lossFunction(newSolution, startTime))  # 这里的comNum不需要改变
        lloss = lossFunction(newSolution, startTime)
        tempcomnum = copy.deepcopy(comNum)  # 存住newSolution的分配情况
        comNum = copy.deepcopy(bestcomnum)  # 加载上solution的分配情况

        # 更新权重、得分
        if r != endF:
            if lloss <= lossFunction(solution, startTime):
                solution = newSolution
                bestcomnum = copy.deepcopy(tempcomnum)
                comNum = copy.deepcopy(bestComNum)  # 加载上bestSolution的分配情况
                if lloss <= lossFunction(bestSolution, startTime):
                    if lloss == lossFunction(bestSolution, startTime):
                        counter += 1  # 计算最优解是第几次不改变值了
                        if counter > countMax:  # “最优解是第几次不改变值”的次数到达阈值就结束程序
                            print("----------------------------------------------------------------")
                            print("已经{}次没有更新过最优解了，提前终止程序".format(countMax))
                            print("----------------------------------------------------------------")
                            iterx = iterxMax
                            break
                    else:
                        counter = 0
                    bestSolution = newSolution
                    bestComNum = copy.deepcopy(tempcomnum)  # 更新bestComNum

                    destroyScoreF[destroyOperatorIndexF] += 1.5  # update the score of the operators
                    repairScoreF[repairOperatorIndexF] += 1.5
                else:

                    destroyScoreF[destroyOperatorIndexF] += 1.2
                    repairScoreF[repairOperatorIndexF] += 1.2

                comNum = copy.deepcopy(tempcomnum)  # 更新comNum的值方便进行下一次运算
            else:
                llloss = lossFunction(solution, startTime)
                comNum = copy.deepcopy(tempcomnum)  # 加载上newSolution的分配情况
                if random.random() < np.exp(
                        (llloss - lossFunction(newSolution,
                                               startTime)) / T):  # the simulated annealing acceptance criteria

                    solution = newSolution
                    bestcomnum = copy.deepcopy(tempcomnum)
                    destroyScoreF[destroyOperatorIndexF] += 0.8
                    repairScoreF[repairOperatorIndexF] += 0.8
                    comNum = copy.deepcopy(tempcomnum)  # 更新comNum的值方便进行下一次运算

                else:
                    destroyScoreF[destroyOperatorIndexF] += 0.6
                    repairScoreF[repairOperatorIndexF] += 0.6
                    comNum = copy.deepcopy(bestcomnum)  # 更新comNum的值方便进行下一次运算

            tempcomnum = copy.deepcopy(comNum)
            comNum = copy.deepcopy(bestcomnum)
            loss_list.append(lossFunction(solution, startTime))
            comNum = copy.deepcopy(tempcomnum)

            wDestroyF[destroyOperatorIndexF] = wDestroyF[destroyOperatorIndexF] * b + (1 - b) * (
                    destroyScoreF[destroyOperatorIndexF] / destroyUseTimesF[destroyOperatorIndexF])
            wRepairF[repairOperatorIndexF] = wRepairF[repairOperatorIndexF] * b + (1 - b) * (
                    repairScoreF[repairOperatorIndexF] / repairUseTimesF[repairOperatorIndexF])
            # update the weight of the operators
        elif r == endF:
            if lloss <= lossFunction(solution, startTime):
                solution = newSolution
                bestcomnum = copy.deepcopy(tempcomnum)
                comNum = copy.deepcopy(bestComNum)  # 加载上bestSolution的分配情况
                if lloss <= lossFunction(bestSolution, startTime):
                    if lloss == lossFunction(bestSolution, startTime):
                        counter += 1  # 计算最优解是第几次不改变值了
                        if counter > countMax:  # “最优解是第几次不改变值”的次数到达阈值就结束程序
                            print("----------------------------------------------------------------")
                            print("已经{}次没有更新过最优解了，提前终止程序".format(countMax))
                            print("----------------------------------------------------------------")
                            iterx = iterxMax
                            break
                    else:
                        counter = 0
                    bestSolution = newSolution
                    bestComNum = copy.deepcopy(tempcomnum)  # 更新bestComNum

                    destroyScoreE[destroyOperatorIndexE] += 1.5  # update the score of the operators
                    repairScoreE[repairOperatorIndexE] += 1.5
                else:

                    destroyScoreE[destroyOperatorIndexE] += 1.2
                    repairScoreE[repairOperatorIndexE] += 1.2

                comNum = copy.deepcopy(tempcomnum)  # 更新comNum的值方便进行下一次运算
            else:
                llloss = lossFunction(solution, startTime)
                comNum = copy.deepcopy(tempcomnum)  # 加载上newSolution的分配情况
                if random.random() < np.exp(
                        (llloss - lossFunction(newSolution,
                                               startTime)) / T):  # the simulated annealing acceptance criteria

                    solution = newSolution
                    bestcomnum = copy.deepcopy(tempcomnum)
                    destroyScoreE[destroyOperatorIndexE] += 0.8
                    repairScoreE[repairOperatorIndexE] += 0.8
                    comNum = copy.deepcopy(tempcomnum)  # 更新comNum的值方便进行下一次运算

                else:
                    destroyScoreE[destroyOperatorIndexE] += 0.6
                    repairScoreE[repairOperatorIndexE] += 0.6
                    comNum = copy.deepcopy(bestcomnum)  # 更新comNum的值方便进行下一次运算

            tempcomnum = copy.deepcopy(comNum)
            comNum = copy.deepcopy(bestcomnum)
            loss_list.append(lossFunction(solution, startTime))
            comNum = copy.deepcopy(tempcomnum)

            wDestroyE[destroyOperatorIndexE] = wDestroyE[destroyOperatorIndexE] * b + (1 - b) * (
                    destroyScoreE[destroyOperatorIndexE] / destroyUseTimesE[destroyOperatorIndexE])
            wRepairE[repairOperatorIndexE] = wRepairE[repairOperatorIndexE] * b + (1 - b) * (
                    repairScoreE[repairOperatorIndexE] / repairUseTimesE[repairOperatorIndexE])

        T = a * T
    iterx += 1
    T = 100

time_3 = time.time()
print()
print(time_2 - time_1)
print(time_3 - time_2)
Bsolution.append(bestSolution)
loss.append(loss_list)


# 计算真实的超时损失
def overtimeLoss(ways, startTime):
    global comNum

    timeloss = 0
    distance = 0
    overtimeloss = 0
    caploss = 0
    no = 0
    printf = False

    # print("ways=",ways)
    for way in ways:
        tempcap = []
        cap = 0
        # print("cap=", cap)
        time = startTime  # 令time=startTime
        lastPoint = -1
        # print("way=",way)
        for point in way:
            # print("当前计算loss的point=",point)
            if (lastPoint != -1):
                # print("lastPoint=",lastPoint)
                # print("point=",point)
                distance += geoDistance(lastPoint, point)
                # print("距离是",geoDistance(lastPoint,point))
                time += int(geoDistance(lastPoint, point) / v)
                # print("用时",int(geoDistance(lastPoint,point)/v))
            lastPoint = point
            if (printf):
                print("到达目标点", point, "时，是", time)

            if point < orderNumberF * 2:
                if (point % 2 == 0):  # 判断是不是起点
                    cap += 1
                    # print("cap=", cap)
                    if cap > maxcap:
                        # print("overcap")
                        caploss += 1  # caploss等于非0的任何正数对于loss来讲都是致命的
                    if (time == startTime):  # 判断是不是每个拖船要取的第一个驳船，若是则无需在此处计算loss，并且令time等于最晚的pick_time
                        time = orderF[int(point / 2)][pick_time_late]
                    else:
                        if (time < orderF[int(point / 2)][pick_time_early]):
                            P = timeLossF(time, orderF[int(point / 2)][pick_time_early])
                            overtimeloss += timeLossF(time, orderF[int(point / 2)][pick_time_early])
                            time = orderF[int(point / 2)][pick_time_late]
                            if (P > 0 and printf):
                                print("目标点", point, "早时", P, "秒")
                        elif (time > orderF[int(point / 2)][pick_time_late]):
                            G = timeLossF(time, orderF[int(point / 2)][pick_time_late])
                            overtimeloss += timeLossF(time, orderF[int(point / 2)][pick_time_late])
                            time += 300  # 5 min 固定驳船时间
                            if (G > 0 and printf):
                                print("目标点", point, "晚时", G, "秒")
                        else:
                            if (time + 300) > orderF[int(point / 2)][pick_time_late]:
                                time += 300
                            else:
                                time = orderF[int(point / 2)][pick_time_late]
                            if (printf):
                                print("目标点", point, "按时到达")

                else:  # 判断是不是终点
                    if (time == startTime):
                        print("wrong in lossFunction（<F*2且为终点）")
                        sys.exit(0)
                    else:
                        cap -= 1
                        # print("cap=", cap)

                        if (time < orderF[int(point / 2)][deliver_time_early]):
                            P = timeLossF(time, orderF[int(point / 2)][deliver_time_early])
                            overtimeloss += timeLossF(time, orderF[int(point / 2)][deliver_time_early])
                            time = orderF[int(point / 2)][pick_time_late]
                            if (P > 0 and printf):
                                print("目标点", point, "早时", P, "秒")
                        elif (time > orderF[int(point / 2)][deliver_time_late]):
                            G = timeLossF(time, orderF[int(point / 2)][deliver_time_late])
                            overtimeloss += timeLossF(time, orderF[int(point / 2)][deliver_time_late])
                            time += 300  # 5min 取消固定驳船时间
                            if (G > 0 and printf):
                                print("目标点", point, "晚时", G, "秒")
                        else:
                            if (time + 300) > orderF[int(point / 2)][deliver_time_late]:
                                time += 300
                            else:
                                time = orderF[int(point / 2)][deliver_time_late]
                            if (printf):
                                print("目标点", point, "按时到达")

            elif point < orderNumberF * 2 + orderNumberE1:
                son = 0
                if (time == startTime):
                    print("wrong in lossFunction（<F*2+E1）")
                    sys.exit(0)
                else:
                    # print(comNum)
                    if point in tempcap:
                        son = 1
                    else:
                        tempcap.append(point)
                    cap -= comNum[point - orderNumberF * 2][no][son]
                    # print("cap=", cap)
                    # print("no=", no)
                    if (time < orderE1[point - orderNumberF * 2][deliver_time_early]):
                        P = timeLossF(time, orderE1[point - orderNumberF * 2][deliver_time_early])
                        overtimeloss += timeLossF(time, orderE1[point - orderNumberF * 2][deliver_time_early])
                        time = orderE1[point - orderNumberF * 2][deliver_time_early]
                        time += 300
                        if (P > 0 and printf):
                            print("目标点", point, "早时", P, "秒")
                    elif (time > orderE1[point - orderNumberF * 2][deliver_time_late]):
                        G = timeLossF(time, orderE1[point - orderNumberF * 2][deliver_time_late])
                        overtimeloss += timeLossF(time, orderE1[point - orderNumberF * 2][deliver_time_late])
                        time += 300  # 5 min 取消固定驳船时间
                        if (G > 0 and printf):
                            print("目标点", point, "晚时", G, "秒")
                    else:
                        time += 300  # 到了之后转头立马走
                        if (printf):
                            print("目标点", point, "按时到达")

            elif point < orderNumberF * 2 + orderNumberE1 + orderNumberE2:
                if (time < orderE2[point - orderNumberF * 2 - orderNumberE1][free_time]):
                    time = orderE2[point - orderNumberF * 2 - orderNumberE1][free_time]
                cap += 1
                # print("cap=", cap)
                if cap > maxcap:
                    # print("overcap")
                    caploss += 1  # caploss等于非0的任何正数对于loss来讲都是致命的
                time += 300  # 5 min 固定驳船时间
        no += 1
        timeloss += time - startTime

    return overtimeloss / 60


# 计算真实的时间损失
def timeLoss(ways, startTime):
    global comNum

    timeloss = 0
    distance = 0
    overtimeloss = 0
    caploss = 0
    no = 0
    printf = False

    # print("ways=",ways)
    for way in ways:
        tempcap = []
        cap = 0
        # print("cap=", cap)
        time = startTime  # 令time=startTime
        lastPoint = -1
        # print("way=",way)
        for point in way:
            # print("当前计算loss的point=",point)
            if (lastPoint != -1):
                # print("lastPoint=",lastPoint)
                # print("point=",point)
                distance += geoDistance(lastPoint, point)
                # print("距离是",geoDistance(lastPoint,point))
                time += int(geoDistance(lastPoint, point) / v)
                # print("用时",int(geoDistance(lastPoint,point)/v))
            lastPoint = point
            if (printf):
                print("到达目标点", point, "时，是", time)

            if point < orderNumberF * 2:
                if (point % 2 == 0):  # 判断是不是起点
                    cap += 1
                    # print("cap=", cap)
                    if cap > maxcap:
                        # print("overcap")
                        caploss += 1  # caploss等于非0的任何正数对于loss来讲都是致命的
                    if (time == startTime):  # 判断是不是每个拖船要取的第一个驳船，若是则无需在此处计算loss，并且令time等于最晚的pick_time
                        time = orderF[int(point / 2)][pick_time_late]
                    else:
                        if (time < orderF[int(point / 2)][pick_time_early]):
                            P = timeLossF(time, orderF[int(point / 2)][pick_time_early])
                            overtimeloss += timeLossF(time, orderF[int(point / 2)][pick_time_early])
                            time = orderF[int(point / 2)][pick_time_late]
                            if (P > 0 and printf):
                                print("目标点", point, "早时", P, "秒")
                        elif (time > orderF[int(point / 2)][pick_time_late]):
                            G = timeLossF(time, orderF[int(point / 2)][pick_time_late])
                            overtimeloss += timeLossF(time, orderF[int(point / 2)][pick_time_late])
                            time += 300  # 5 min 固定驳船时间
                            if (G > 0 and printf):
                                print("目标点", point, "晚时", G, "秒")
                        else:
                            if (time + 300) > orderF[int(point / 2)][pick_time_late]:
                                time += 300
                            else:
                                time = orderF[int(point / 2)][pick_time_late]
                            if (printf):
                                print("目标点", point, "按时到达")

                else:  # 判断是不是终点
                    if (time == startTime):
                        print("wrong in lossFunction（<F*2且为终点）")
                        sys.exit(0)
                    else:
                        cap -= 1
                        # print("cap=", cap)

                        if (time < orderF[int(point / 2)][deliver_time_early]):
                            P = timeLossF(time, orderF[int(point / 2)][deliver_time_early])
                            overtimeloss += timeLossF(time, orderF[int(point / 2)][deliver_time_early])
                            time = orderF[int(point / 2)][pick_time_late]
                            if (P > 0 and printf):
                                print("目标点", point, "早时", P, "秒")
                        elif (time > orderF[int(point / 2)][deliver_time_late]):
                            G = timeLossF(time, orderF[int(point / 2)][deliver_time_late])
                            overtimeloss += timeLossF(time, orderF[int(point / 2)][deliver_time_late])
                            time += 300  # 5min 取消固定驳船时间
                            if (G > 0 and printf):
                                print("目标点", point, "晚时", G, "秒")
                        else:
                            if (time + 300) > orderF[int(point / 2)][deliver_time_late]:
                                time += 300
                            else:
                                time = orderF[int(point / 2)][deliver_time_late]
                            if (printf):
                                print("目标点", point, "按时到达")

            elif point < orderNumberF * 2 + orderNumberE1:
                son = 0
                if (time == startTime):
                    print("wrong in lossFunction（<F*2+E1）")
                    sys.exit(0)
                else:
                    # print(comNum)
                    if point in tempcap:
                        son = 1
                    else:
                        tempcap.append(point)
                    cap -= comNum[point - orderNumberF * 2][no][son]
                    # print("cap=", cap)
                    # print("no=", no)
                    if (time < orderE1[point - orderNumberF * 2][deliver_time_early]):
                        P = timeLossF(time, orderE1[point - orderNumberF * 2][deliver_time_early])
                        overtimeloss += timeLossF(time, orderE1[point - orderNumberF * 2][deliver_time_early])
                        time = orderE1[point - orderNumberF * 2][deliver_time_early]
                        time += 300
                        if (P > 0 and printf):
                            print("目标点", point, "早时", P, "秒")
                    elif (time > orderE1[point - orderNumberF * 2][deliver_time_late]):
                        G = timeLossF(time, orderE1[point - orderNumberF * 2][deliver_time_late])
                        overtimeloss += timeLossF(time, orderE1[point - orderNumberF * 2][deliver_time_late])
                        time += 300  # 5 min 取消固定驳船时间
                        if (G > 0 and printf):
                            print("目标点", point, "晚时", G, "秒")
                    else:
                        time += 300  # 到了之后转头立马走
                        if (printf):
                            print("目标点", point, "按时到达")

            elif point < orderNumberF * 2 + orderNumberE1 + orderNumberE2:
                if (time < orderE2[point - orderNumberF * 2 - orderNumberE1][free_time]):
                    time = orderE2[point - orderNumberF * 2 - orderNumberE1][free_time]
                cap += 1
                # print("cap=", cap)
                if cap > maxcap:
                    # print("overcap")
                    caploss += 1  # caploss等于非0的任何正数对于loss来讲都是致命的
                time += 300  # 5 min 固定驳船时间
        no += 1
        timeloss += time - startTime

    return timeloss / 60


# 计算真实的距离损失
def disLoss(ways, startTime):
    distance = 0
    for way in ways:
        lastPoint = -1
        for point in way:
            if (lastPoint != -1):
                distance += geoDistance(lastPoint, point)
            lastPoint = point
    y = 1
    loss = distance
    return loss / 1000  # 返回km


# 计算Bsolution[0]的timeLoss、overtimeLoss
comNum = copy.deepcopy(firstcomnum)
timeloss1 = timeLoss(Bsolution[0], startTime)
overtimeloss1 = overtimeLoss(Bsolution[0], startTime)

# 计算Bsolution[1]的timeLoss、overtimeLoss
comNum = copy.deepcopy(bestComNum)
timeloss2 = timeLoss(Bsolution[1], startTime)
overtimeloss2 = overtimeLoss(Bsolution[1], startTime)

print("总单数", OrderNumber)
print()
print("contrast:", Bsolution[0])
print("timeloss:", timeloss1, "min")
print("disloss", disLoss(Bsolution[0], startTime), "km")
print("overtimeloss", overtimeloss1, "min")
print("totalloss:",
      timeloss1 + disLoss(Bsolution[0], startTime) + overtimeloss1)

print()

print("experiment:", Bsolution[1])

print("timeloss:", timeloss2, "min")
print("disloss", disLoss(Bsolution[1], startTime), "km")
print("overtimeloss", overtimeloss2, "min")
print("totalloss:",
      timeloss2 + disLoss(Bsolution[1], startTime) + overtimeloss2)
print()

saveTime = timeloss1 - timeloss2
p = saveTime / timeloss1 * 100
pa = (saveTime / OrderNumber)
if saveTime > 0:
    print("time improvment:", saveTime, "min. 每单耗时平均减少: ", "%.2f" % pa, "min", "%.2f" % p, "%")
else:
    print("time reduction:", -saveTime, "min. 每单耗时平均增加: ", "%.2f" % -pa, "min", "%.2f" % -p, "%")

saveDis = disLoss(Bsolution[0], startTime) - disLoss(Bsolution[1], startTime)
q = saveDis / disLoss(Bsolution[0], startTime) * 100
qa = (saveDis / OrderNumber)
print("distance improvment:", saveDis, "km. 每单路程平均减少:", "%.2f" % qa, "km", "%.2f" % q, "%")

saveoverTime = overtimeloss1 - overtimeloss2
o = saveoverTime / overtimeloss1 * 100
oa = (saveoverTime / OrderNumber)
print("overtime improvment:", saveoverTime, "min. 每单超时罚时平均减少: ", "%.2f" % oa, "min", "%.2f" % o, "%")

print("wDestroyF=", wDestroyF)
print("wRepairF=", wRepairF)
print("destroyUseTimesF=", destroyUseTimesF)
print("repairUseTimesF=", repairUseTimesF)
print("-----------------------------------------------------------------------------------------------------------------")
print("wDestroyE=", wDestroyE)
print("wRepairE=", wRepairE)
print("destroyUseTimesE=", destroyUseTimesE)
print("repairUseTimesE=", repairUseTimesE)

# 迭代最优解迭代过程画图
plt.figure(figsize=(10, 10))
plt.hlines(xmin=1, xmax=len(loss[1]), y=loss[0], colors="g")
plt.plot(loss[1], 'r')
plt.legend(['initial', 'final'])
plt.xlabel('epoch')
plt.ylabel('loss')

# 用于呈现骑手的路径  # 数据读取错误！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
colorboard = ['#000000', '#0000FF', '#A52A2A', '#87CEEB', '#F08080', '#7CFC00', '#FF69B4', '#87CEEB', '#87CEEB',
              '#ADD8E6', '#FA8072', '#ADD8E6', ]

plt.figure()  # 再画拆分之前的解

# 读取全部订单
path = "inlandriver_order/order6.csv"
orderall = pd.read_csv(path, header=0, usecols=["pick_lng", "pick_lat", "custome_lng", "custome_lat",
                                                "pick_time_early", "pick_time_late", "deliver_time_early",
                                                "deliver_time_late", "need_deliver_amount", "free_time"])
orderF = orderall[orderall["pick_time_late"].notnull()]
orderE1 = orderall[orderall["need_deliver_amount"].notnull()]
orderE2 = orderall[orderall["free_time"].notnull()]

# 读取F类型订单
orderF = orderF.values
orderF = np.array(orderF)  # 将dataframe类型的数据保存为numpy数组

# 读取E类型订单的终点部分
orderE1 = orderE1.values
orderE1 = np.array(orderE1)  # 将dataframe类型的数据保存为numpy数组

# 读取E类型订单的起点部分
orderE2 = orderE2.values
orderE2 = np.array(orderE2)  # 将dataframe类型的数据保存为numpy数组

# 模型设置参数
orderNumberF = np.shape(orderF)[0]
orderNumberE1 = np.shape(orderE1)[0]
orderNumberE2 = np.shape(orderE2)[0]
OrderNumber = orderNumberF + orderNumberE1  # OrderNumber为总单数
pointNumber = orderNumberF * 2 + orderNumberE1 + orderNumberE2  # pointNumber后面将用于读取所有的目标点

# 读取F类型订单目标点信息
targetPoint = np.empty([pointNumber, 2], dtype=float)  # 生成了一个42*2的多维数组，一列存点的经度，一列存点的纬度
j = 0
for i in range(orderNumberF):  # 读取各起终点的经纬度
    targetPoint[j][0] = orderF[i][0]
    targetPoint[j][1] = orderF[i][1]
    j += 1
    targetPoint[j][0] = orderF[i][2]
    targetPoint[j][1] = orderF[i][3]
    j += 1

# 读取E类型订单起点信息
for i in range(orderNumberE1):
    targetPoint[j][0] = orderE1[i][2]
    targetPoint[j][1] = orderE1[i][3]
    j += 1

# 读取E类型订单终点信息
for i in range(orderNumberE2):
    targetPoint[j][0] = orderE2[i][0]
    targetPoint[j][1] = orderE2[i][1]
    j += 1

for x in range(2):
    # print("Bsolution[x]=", Bsolution[x])
    for way, num, c in zip(Bsolution[x], range(len(bestSolution)), colorboard):
        j = -1
        for i in way:
            # print("i=",i)
            if (j == -1):
                plt.scatter(targetPoint[i][0], targetPoint[i][1], color='r', s=5)
                # print("打印目标点",i)
                j = i
            else:
                plt.scatter(targetPoint[i][0], targetPoint[i][1], color='r', s=5)
                # print("打印目标点",i)
                plt.plot([targetPoint[i][0], targetPoint[j][0]], [targetPoint[i][1], targetPoint[j][1]],
                         color=c)  # 第一个数组全是横坐标第二个全是纵坐标
                j = i
    plt.savefig('ex6\'s {}.png'.format(x + 1), dpi=1800)
    plt.show()